Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字长读取数据_Java_Android_Sockets - Fatal编程技术网

Java 套接字长读取数据

Java 套接字长读取数据,java,android,sockets,Java,Android,Sockets,这是我的代码: private String receiveData(String sjson) { Log.i(TAG,"send request: " + sjson); String jstr=""; try { OutputStream out = s.getOutputStream(); out.write(sjson.getBytes()); out.flush()

这是我的代码:

private String receiveData(String sjson) {
        Log.i(TAG,"send request: " + sjson);
        String jstr="";
        try {
            OutputStream out = s.getOutputStream();
            out.write(sjson.getBytes());
            out.flush();
            //out.close();
            Log.v(TAG,"sended data");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            char[] cbuf = new char[1];
            input.read(cbuf);                   
            String size = new String(cbuf);
            while (input.read(cbuf) != 0) {
                if((new String(cbuf)).equals("{") == true) 
                    break;
                size = size + new String(cbuf);
            }
            char[] jbuf = new char[Integer.valueOf(size)];
            input.read(jbuf);
            jstr = "{" + new String(jbuf);
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
        Log.d(TAG,"responce: " + jstr);
        return jstr;
    }

    public void connectSocket() {
        Log.v(TAG,"connecting Socket: "+URL+":"+PORT);
        try {
            s = new Socket(URL, PORT);
            Log.v(TAG,"connect Socket!");
            ERROR_CODE = 0;
        }catch (Exception e) {
            Log.e(TAG,e.toString());
            ERROR_CODE = ERROR_SOCKET_CONNECT_SUCCESSFULL;
        }
        Log.e(TAG,getErrorMsg(ERROR_CODE));
    }

    public void closeSocket() {
        Log.v(TAG,"closeSocket");
        try {
            s.close();
        }catch (Exception e) {
            Log.e(TAG,e.toString());
        }
    }
在服务器上,答案不到一秒钟。在客户端,读取数据前经过1分钟

应用程序在
input.read(cbuf)停止等待回答

日志:


为什么阅读答案要花这么长时间?

你到底希望这种方法做什么?它有缺陷,它做了它应该做的事情

  • 创建InputStreamReader时,应指定编码/字符集
  • 为什么要从开始到“{”一个字符一个字符地读
  • 为什么要在点击“{”之前为读取的每个字符创建一个字符串
  • 为什么要在循环中附加字符串?如果必须附加字符串,请使用StringBuilder
  • input.read返回一个整数,表示您已接收的字节/字符数 它永远不能保证它会填满缓冲区,因此您可能无法获取所有数据
  • 你为什么不关闭资源

…现在来看看为什么速度会慢。服务器正在刷新数据吗?如果没有,请确保服务器正在刷新数据。

谢谢,我都正确。服务器正在刷新数据,在这个问题中。我注意到当我读取1个字节时,数据读取1秒。但随后我读取2个字节,然后数据重新读取1分钟。
05-23 06:35:17.540: VERBOSE/Utilits(358): Auth: 77.221.129.100:10598
05-23 06:35:17.660: INFO/Utilits(358): send request: 0119{"data":{"password":"12345","imei":"000000000000001"},"method":"login"}
05-23 06:36:17.909: DEBUG/Utilits(358): responce: {"response":{"success":true,"user":{"id":"6","properties":{"auto":"model":"audi","color":"ffff","number":"td123r"}},"is_driver":"1"}}}