2013-12-09 148 views
0

我一直在研究一個程序,它通過套接字連接發送和接收一個字符串到服務器。我用Asynctask打開連接併發送適當的命令,但不管什麼都沒有發生!我知道發送到服務器的命令是絕對正確的,服務器信息是正確的,所以我很難過!Android Asynctask套接字連接

這裏是我的Java代碼

new internetRoutesRetrieve().execute(""); 

private class internetRoutesRetrieve extends AsyncTask<String, Void, String> { 
      @Override 
      protected String doInBackground(String... parms) { 
       String userNameSend = userName; 
       String response = null; 
       try { 
        Socket client = new Socket("Hidden", Hidden); 
        DataOutputStream out = new DataOutputStream(client.getOutputStream()); 
        DataInputStream in = new DataInputStream(client.getInputStream()); 
        String command = "SEARCH " + userNameSend; 
        out.writeUTF(command); 
        out.flush(); 
        String responseServer = in.readUTF(); 
        out.close(); 
        in.close(); 
        response = responseServer; 
       } catch (UnknownHostException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return response; 
      } 
      @Override 
      protected void onPostExecute(String result) { 
        //This is always null! 
        System.out.println(result); 
       responseIds = result; 
      } 
      @Override 
      protected void onPreExecute() { 


      } 
      @Override 
      protected void onProgressUpdate(Void... values) { 

      } 
     } 
} 

我logcat的,如果有幫助

12-09 02:58:52.925: W/System.err(17598): java.io.EOFException 
12-09 02:58:53.056: W/System.err(17598): at libcore.io.Streams.readFully(Streams.java:83) 
12-09 02:58:53.056: W/System.err(17598): at java.io.DataInputStream.readFully(DataInputStream.java:120) 
12-09 02:58:53.066: W/System.err(17598): at java.io.DataInputStream.decodeUTF(DataInputStream.java:195) 
12-09 02:58:53.066: W/System.err(17598): at java.io.DataInputStream.decodeUTF(DataInputStream.java:190) 
12-09 02:58:53.066: W/System.err(17598): at java.io.DataInputStream.readUTF(DataInputStream.java:186) 
12-09 02:58:53.066: W/System.err(17598): at com.example.app.RouteSelection$internetRoutesRetrieve.doInBackground(RouteSelection.java:145) 
12-09 02:58:53.066: W/System.err(17598): at com.example.app.RouteSelection$internetRoutesRetrieve.doInBackground(RouteSelection.java:1) 
12-09 02:58:53.066: W/System.err(17598): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
12-09 02:58:53.066: W/System.err(17598): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
12-09 02:58:53.066: W/System.err(17598): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
12-09 02:58:53.066: W/System.err(17598): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
12-09 02:58:53.066: W/System.err(17598): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
12-09 02:58:53.066: W/System.err(17598): at java.lang.Thread.run(Thread.java:856) 

感謝所有幫助:)

回答

0

的EOFException類信號,即輸入流之前到達其終點它是預期的。看起來你的服務器沒有給你足夠的數據或無效的數據。

請確保您的服務器上使用了outputStream.writeUTF(...)。