Android Asynctask套接字连接

时间:2013-12-09 03:01:06

标签: java android sockets android-asynctask

我一直致力于通过套接字连接向服务器发送和接收字符串的程序。我已经使用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)

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

EOFException表示输入流在预期之前到达其末尾。您的服务器似乎没有向您发送足够的数据或无效数据。

确保在服务器上使用outputStream.writeUTF(...)。