Android SocketTimeoutException原因

时间:2016-04-14 17:21:10

标签: android rest api-design

我想在我的应用程序中使用REST API。它会为此应用程序的所有请求抛出SocketTimeoutException

Logcat输出:(你也可以在这里看到漂亮的格式:http://pastebin.com/FbZU6wRd

  

04-14 18:58:46.769 22839-22839 / kovacsdev.hu.facec W / AsyncHttpClient:   传递的contentType将被忽略,因为HttpEntity设置内容   型号04-14 18:58:46.769 22839-22839 / kovacsdev.hu.facec D / dalvikvm:   create interp thread:stack size = 32KB 04-14 18:58:46.770   22839-22839 / kovacsdev.hu.facec D / dalvikvm:创建新主题04-14   18:58:46.770 22839-22839 / kovacsdev.hu.facec D / dalvikvm:新主题   创建于04-14 18:58:46.770 22839-22839 / kovacsdev.hu.facec D / dalvikvm:   更新主题列表04-14 18:58:46.770 22839-23323 / kovacsdev.hu.facec   D / dalvikvm:threadid = 20:interp stack at 0x53f36000 04-14 18:58:46.770   22839-23323 / kovacsdev.hu.facec D / dalvikvm:threadid = 20:源自   interp 04-14 18:58:46.770 22839-22839 / kovacsdev.hu.facec D / dalvikvm:   开始新主题04-14 18:58:46.771 22839-23323 / kovacsdev.hu.facec   D / dalvikvm:threadid = 20:通知调试器04-14 18:58:46.771   22839-23323 / kovacsdev.hu.facec D / dalvikvm:threadid = 20   (pool-2-thread-1):调用run()04-14 18:58:46.797   22839-22839 / kovacsdev.hu.facec I / SurfaceTextureClient:   [STC :: queueBuffer](this:0x50c8b858)fps:0.30,dur:6595.54,   最大值:6086.69,最小值:508.85 04-14 18:58:46.797   22839-22839 / kovacsdev.hu.facec I / SurfaceTextureClient:   [STC :: queueBuffer] this:0x50c8b858,api:1,最后一个队列时间   逝去时间:6086.69 04-14 18:58:47.154 22839-23323 / kovacsdev.hu.facec   D / libc-netbsd:getaddrinfo:api.kairos.com从代理>>获得结果   04-14 18:58:47.155 22839-23323 / kovacsdev.hu.facec I / System.out:   propertyValue:true 04-14 18:58:47.156 22839-23323 / kovacsdev.hu.facec   I / System.out:[socket] [0]连接   /50.17.167.207:80 ;LocalPort = 55439(10000)04-14 18:58:47.157   22839-23323 / kovacsdev.hu.facec I / System.out:   [CDS] connect [/50.17.167.207:80] tm:10 04-14 18:58:47.158   22839-23323 / kovacsdev.hu.facec D / Posix:[Posix_connect Debug]进程   kovacsdev.hu.facec:80 04-14 18:58:47.158   22839-23323 / kovacsdev.hu.facec I / System.out:   [socket] [/ 192.168.199.102:55439]连接04-14 18:58:47.158   22839-23323 / kovacsdev.hu.facec I / System.out:[CDS] rx timeout:10000   04-14 18:58:47.159 22839-23323 / kovacsdev.hu.facec W / System.err:rto   值太小:0 04-14 18:58:47.164 22839-23323 / kovacsdev.hu.facec   I / System.out:> doSendRequest 04-14 18:58:47.167   22839-22847 / kovacsdev.hu.facec D / jdwp:processIncoming 04-14   18:58:47.167 22839-22847 / kovacsdev.hu.facec D / jdwp:handlePacket:   cmd = 0x1,cmdSet = 0xC7,len = 0x14,id = 0x4000013E,flags = 0x0,dataLen = 0x9   04-14 18:58:47.167 22839-22847 / kovacsdev.hu.facec D / jdwp:   sendBufferedRequest:len = 0x34 04-14 18:58:47.254   22839-23323 / kovacsdev.hu.facec I / System.out:

这里发生了确切的问题。

  

04-14 18:58:57.248 22839-23323 / kovacsdev.hu.facec I / System.out:   [CDS] EAGAIN或EWOULDBLOCK在Recvfrom 04-14 18:58:57.249   22839-23323 / kovacsdev.hu.facec I / System.out:[CDS]读取字节为0 04-14   18:58:57.250 22839-23323 / kovacsdev.hu.facec I / System.out:   [CDS] close [55439] 04-14 18:58:57.251 22839-23323 / kovacsdev.hu.facec   I / System.out:close [socket] [/ 0.0.0.0:55439] 04-14 18:58:57.252   22839-23323 / kovacsdev.hu.facec I / System.out:   例如:java.net.SocketTimeoutException 04-14 18:58:57.253   22839-23323 / kovacsdev.hu.facec W / System.err:   java.net.SocketTimeoutException 04-14 18:58:57.268   22839-23323 / kovacsdev.hu.facec W / System.err:at   java.net.PlainSocketImpl.read(PlainSocketImpl.java:495)

代码段:

    public void onClick(View v) {
        Bitmap image = BitmapFactory.decodeFile(file);
        String subjectId = user;
        String galleryId = "users";
        String selector = "FULL";
        String multipleFaces = "false";
        String minHeadScale = "0.25";
        try {
            myKairos.enroll(image,
                    subjectId,
                    galleryId,
                    selector,
                    multipleFaces,
                    minHeadScale,
                    listener);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

来自Kairos类的方法。

/*
 * Enroll subject into gallery (Image)
 */
public void enroll(Bitmap image,
                   String subjectId,
                   String galleryId,
                   String selector,
                   String multipleFaces,
                   String minHeadScale,
                   final KairosListener callback)  throws JSONException, UnsupportedEncodingException {

    AsyncHttpClient client = new AsyncHttpClient();

    AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            // called before request is started
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            // called when response HTTP status is "200 OK"
            String responseString = new String(response);
            callback.onSuccess(responseString);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            String responseString = new String(errorResponse);
            callback.onFail(responseString);

        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }

    };

    JSONObject jsonParams = new JSONObject();
    jsonParams.put("image", base64FromBitmap(image));
    jsonParams.put("subject_id", subjectId);
    jsonParams.put("gallery_name", galleryId);

    if(selector != null) {
        jsonParams.put("selector", selector);
    }

    if(minHeadScale != null) {
        jsonParams.put("minHeadScale", minHeadScale);
    }

    if(multipleFaces != null) {
        jsonParams.put("multiple_faces", multipleFaces);
    }

    StringEntity entity = new StringEntity(jsonParams.toString());
    client.addHeader("app_id", my_app_id);
    client.addHeader("app_key", my_api_key);
    client.post(my_context, "http://api.kairos.com/enroll", entity, "application/json", responseHandler);

}

1 个答案:

答案 0 :(得分:2)

按照此处http://loopj.com/android-async-http/建议创建静态Http客户端,但也将上下文传递给get / post方法。