响应没有进入(android套接字编程/ tcp/ip套接字编程)

响应没有进入(android套接字编程/ tcp/ip套接字编程)

问题描述:

我正在开发一个Android应用程序,我必须在服务器上发送位置数据并获取作业数据作为响应,这样所有通信都会生成但当我试图从服务器读取没有任何输出的响应,并且没有任何响应来临时,我困扰于一件事情,所以请帮助我摆脱困境。以下是我的代码。提前致谢。响应没有进入(android套接字编程/ tcp/ip套接字编程)

public static class MyClientTask extends AsyncTask<Void, Void, Void> { 
     String dstAddress; 
     int dstPort; 
     String response = ""; 
     String s; 
     String red; 
     String loc; 
     String msg; 
     Socket socket = null; 
     DataOutputStream dataOutputStream = null; 
     DataInputStream dataInputStream = null; 
     InputStream is=null; 
     BufferedReader br=null; 
     public MyClientTask(String addr, int port,String msg){ 
     dstAddress = addr; 
     dstPort = port; 
     loc=msg; 
     } 
     @Override 
     protected Void doInBackground(Void... arg0) { 

      try { 
        socket = new Socket(dstAddress, dstPort); 
        socket.setKeepAlive(true); 
        dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
        is=socket.getInputStream(); 
       socket.setSoTimeout(60*1000); 
        dataInputStream = new DataInputStream(is); 

        Log.i("socket connect","socket OK"); 
        dataOutputStream.writeUTF(loc); 
        while (dataInputStream == null) 
        { 
         ////this part is not working 
         Log.i("DEV", "sleep "+is.available()); 
         android.os.SystemClock.sleep(100); 
        } 
        br=new BufferedReader(new InputStreamReader(dataInputStream)); 
        String st = null; 
        while(socket.isConnected()){ 
         st = br.readLine(); 
         } 
        Log.w("server response", "says Server = " + st); 

        Dbase db2=new Dbase(mcontext); 
        db2.addresponse(new info(st)); 
        Log.w("second time server response", "says 2ndTime Server = " + st); 
       } catch (UnknownHostException e) { 
        Log.e("at exception", "at thread unknownHost " + e.getMessage()); 
        e.printStackTrace(); 
       } catch (IOException e) { 
        Log.e("io exception", "at thread IO " + e.getMessage()); 
        e.printStackTrace(); 
       } 

       finally{ 
        Log.i("on finally block", "finally"); 
        if (dataOutputStream != null){ 
         try { 
          dataOutputStream.close(); 
         } catch (IOException e) { 
          Log.e("io eception", "at thread dataoutput IO " + e.getMessage()); 
          e.printStackTrace(); 
         } 
        } 

        if (dataInputStream != null){ 
         try { 
          dataInputStream.close(); 
         } catch (IOException e) { 
          Log.e("data input exception", "at thread datainput IO " + e.getMessage()); 
          e.printStackTrace(); 
         } 
        } 
        if (socket != null){ 
         try { 
          Log.i("socket", "socket closed"); 
          socket.close(); 
         } catch (IOException e) { 
          Log.e("socket exception", "at thread finally IO " + e.getMessage()); 
          e.printStackTrace(); 
         } 
        } 
       } 
       return null; 
      } 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       //displayProgressBar("Downloading..."); 
      } 


      @Override 
      protected void onPostExecute(Void result) { 
       super.onPostExecute(result); 
      } 
     } 
+0

你有义务使用套接字吗?我的意思是,许多人已经发明了车轮,因为有许多不同的和更易于使用的方式... – EpicPandaForce 2015-03-03 12:24:56

+0

如果可能的话,对于位置数据使用Web服务而不是套接字编程。 – 2015-03-03 12:27:48

+0

是的,我必须开发这个应用程序使用套接字你有任何关于我的问题的想法,你必须与我分享.. – 2015-03-03 12:27:54

使用不当InputStream.available方法。此方法不会尝试检索数据。实际上,此方法的大多数实现始终返回0.请参阅Inputstream available reference。 我建议您只删除可用的检查,并根据需要让readLine块。 BufferedReader.ready也是如此 - 通常这并不表示在尝试阅读时您将获得任何数据,因此此调用也无用。

+0

先生实际上服务器不发送特定时间或onrequest响应这就是为什么需要检查输入流可用性在这种情况下InputStream.available()是需要的.. – 2015-03-04 04:21:08

+0

InputStream.available不能帮助你在这里。在你的情况下,它可能总是返回0.如果服务器没有响应套接字超时将发生。如果你想要,你可以改变套接字超时值。 – Okas 2015-03-04 07:17:59

+0

我删除InputStream.available检查条件它也没有给我从服务器响应 – 2015-03-04 07:33:53