http超时限制可以用一下两句:

 HttpConnectionParams.setConnectionTimeout(post.getParams(), CONNECTION_TIOMOUT);
 HttpConnectionParams.setSoTimeout(post.getParams(), CONNECTION_TIOMOUT);
但是这只能限定连接超时和soket超时,对于dns查找超时和传输内容过多而导致的超时是无能为力的。

应付这些超时,可以使用一条额外的超时检查线程来把超时的http请求线程强制interrupt。

调用httpPostRequest的线程会等待网络线程完成,若网络线程在限定时间内完成请求,则调用notify唤醒等待线程。若网络线程超时,检查线程调用interrupt,中止网络线程,同时调用notify唤醒等待线程。

public static String httpPostRequest(String url, JSONObject jsonObject ) {
        try {
            final HttpClient httpClient = new DefaultHttpClient();

            final HttpPost post = new HttpPost(url);
            post.setHeader("Content-Type", "application/json");
            post.setEntity(new StringEntity(jsonObject.toString()));
            HttpConnectionParams.setConnectionTimeout(post.getParams(), CONNECTION_TIOMOUT);
            HttpConnectionParams.setSoTimeout(post.getParams(), CONNECTION_TIOMOUT);

            HttpExecutehread th = new HttpExecutehread(httpClient, post);
            th.start();

            new Timer(true).schedule(new CheckTimeOutTask(th), OVERALL_TIOMOUT);

            synchronized (th) {
                th.wait();
            }
            LogUtils.d(TAG, "after waite " + th);

            if (th.mHttpResponse != null && th.mHttpResponse.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(th.mHttpResponse.getEntity(), "utf-8");
                LogUtils.d(TAG, "get post result");
                return result;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static class CheckTimeOutTask extends TimerTask {
        private HttpExecutehread th;
        public CheckTimeOutTask(HttpExecutehread th) {
            this.th = th;
        }

        @Override
        public void run() {
            if (th != null && th.mHttpResponse == null) {
                th.interrupt();
                synchronized (th) {
                    th.notify();
                }
                LogUtils.d(TAG, "post thread " + th + " interrupt and notify");
            }
        }
    }

    private static class HttpExecutehread extends Thread {
        private HttpPost mHttpPost;
        private HttpClient mHttpClient;
        public HttpResponse mHttpResponse;

        public HttpExecutehread(HttpClient httpClient, HttpPost httpPost) {
            mHttpClient = httpClient;
            mHttpPost = httpPost;
        }

        @Override
        public void run() {
            try {
                mHttpResponse = mHttpClient.execute(mHttpPost);
            } catch (IllegalArgumentException ec) {
                LogUtils.d(TAG, ec.toString());
                interrupted();
            } catch (ClientProtocolException e) {
                LogUtils.d(TAG, e.toString());
                interrupted();
            } catch (IOException e) {
                LogUtils.d(TAG, e.toString());
                interrupted();
            }finally {
                LogUtils.d(TAG, "post notify");
                synchronized (this) {
                    notify();
                }
            }
        }
    }


Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐