Android SocketTimeoutException 原因

Android SocketTimeoutException reasons

我想在我的应用程序中使用 REST API。它为我从这个应用程序发出的所有请求抛出 SocketTimeoutException

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

04-14 18:58:46.769 22839-22839/kovacsdev.hu.facec W/AsyncHttpClient: Passed contentType will be ignored because HttpEntity sets content type 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: create new thread 04-14 18:58:46.770 22839-22839/kovacsdev.hu.facec D/dalvikvm: new thread created 04-14 18:58:46.770 22839-22839/kovacsdev.hu.facec D/dalvikvm: update thread list 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: created from interp 04-14 18:58:46.770 22839-22839/kovacsdev.hu.facec D/dalvikvm: start new thread 04-14 18:58:46.771 22839-23323/kovacsdev.hu.facec D/dalvikvm: threadid=20: notify debugger 04-14 18:58:46.771 22839-23323/kovacsdev.hu.facec D/dalvikvm: threadid=20 (pool-2-thread-1): calling run() 04-14 18:58:46.797 22839-22839/kovacsdev.hu.facec I/SurfaceTextureClient: [STC::queueBuffer] (this:0x50c8b858) fps:0.30, dur:6595.54, max:6086.69, min:508.85 04-14 18:58:46.797 22839-22839/kovacsdev.hu.facec I/SurfaceTextureClient: [STC::queueBuffer] this:0x50c8b858, api:1, last queue time elapsed:6086.69 04-14 18:58:47.154 22839-23323/kovacsdev.hu.facec D/libc-netbsd: getaddrinfo: api.kairos.com get result from proxy >> 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] connection /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]Process kovacsdev.hu.facec :80 04-14 18:58:47.158 22839-23323/kovacsdev.hu.facec I/System.out: [socket][/192.168.199.102:55439] connected 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 value is too small: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 or EWOULDBLOCK in Recvfrom 04-14 18:58:57.249 22839-23323/kovacsdev.hu.facec I/System.out: [CDS]read byte is 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: ex: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 class.

/*
 * 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);

}

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