简体   繁体   English

Android Asynctask套接字连接

[英]Android Asynctask Socket Connection

I have been working on a program that sends and receives a string via socket connection to a server. 我一直在研究通过套接字连接到服务器发送和接收字符串的程序。 I have used Asynctask to open the connection and send the appropriate commands but no matter what nothing happens! 我已经使用Asynctask打开连接并发送适当的命令,但是无论如何都不会发生! I know the command sending to the server is definitely correct and the server information is correct so I am stumped! 我知道发送到服务器的命令绝对正确,并且服务器信息正确,所以我很困惑!

Here is my java code 这是我的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) {

            }
        }
}

My LogCat if it helps 我的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)

Thanks for any help :) 谢谢你的帮助 :)

An EOFException signals, that the input stream reached its end before it was expected. 一个EOFException信号表明输入流在未达到预期的情况下到达了末尾。 It seems like your server doesn't send you enough data or invalid data. 您的服务器似乎没有向您发送足够的数据或无效的数据。

Make sure you use outputStream.writeUTF(...) on your server. 确保在服务器上使用outputStream.writeUTF(...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM