我想在我的应用程序中使用rest api。它会为我所有来自此应用程序的请求抛出SocketTimeoutException
Logcat输出:(您也可以在这里看到漂亮的格式:http://pastebin.com/FbZU6wRd
04-14 18:58:46.769 22839-22839/kovacsdev.hu.facec w/asynchttp客户端:
传递的ContentType将被忽略,因为HttpEntity设置内容
型号04-14 18:58:46.769 22839-22839/kovacsdev.hu.facec d/dalvikvm:
创建interp线程:堆栈大小=32kb04-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堆栈位于0x53f360000 04-14 18:58:46.770
22839-23323/kovacsdev.hu.facec d/dalvikvm:threadid=20:创建自
国际电话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/表面结构客户端:
[STC::队列缓冲区](this:0x50c8b858)fps:0.30,dur:6595.54,
最大值:6086.69,最小值:508.85 04-14 18:58:46.797
22839-22839/kovacsdev.hu.facec i/表面结构客户端:
[stc::queuebuffer]这个: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输入/系统输出:
属性值:true 04-14 18:58:47.156 22839-23323/kovacsdev.hu.facec
I/System.Out:[Socket][0]连接
/50.17.167.207:80;本地端口=55439(10000)04-14 18:58:47.157
22839-23323/kovacsdev.hu.facec输入/系统输出:
[CDS]连接[/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输入/系统输出:
[插座]/192.168.199.102:55439]已连接04-14 18:58:47.158
22839-23323/kovacsdev.hu.facec i/system.out:[cds]rx超时: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
输入/系统输出:>DOSEndrequest 04-14 18:58:47.167
22839-22847/kovacsdev.hu.facec d/jdwp:处理传入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:
发送缓冲请求:len=0x34 04-14 18:58:47.254
22839-23323/kovacsdev.hu.facec输入/系统输出:
问题就在这里。
04-14 18:58:57.248 22839-23323/kovacsdev.hu.facec输入/系统输出:
[CDS]Eagain或eWouldblock in 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输入/系统输出:
[CDS]关闭[55439]04-14 18:58:57.251 22839-23323/kovacsdev.hu.facec
I/system.out:关闭[插座]/0.0.0:55439]04-14 18:58:57.252
22839-23323/kovacsdev.hu.facec输入/系统输出:
示例:java.net.socketTimeoutException 04-14 18:58:57.253
22839-23323/kovacsdev.hu.facec,带系统。错误:
java.net.socketTimeoutException 04-14 18:58:57.268
22839-23323/kovacsdev.hu.facec w/system.err:在
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);

}

最佳答案

按照这里的建议创建一个静态http客户端http://loopj.com/android-async-http/,同时将上下文传递给get/post方法。

关于android - Android SocketTimeoutException原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36629873/

10-10 23:46