HttpURLConnection中的SocketTimeoutException在getResponseCode中随机 [英] SocketTimeoutException in HttpURLConnection randomly in getResponseCode

查看:85
本文介绍了HttpURLConnection中的SocketTimeoutException在getResponseCode中随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在con.getResponseCode()中遇到以下错误

I am getting following error in con.getResponseCode()

java.net.SocketTimeoutException: failed to connect to example.com (port 80) after 3000ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:223)
at libcore.io.IoBridge.connect(IoBridge.java:127)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:475)
at java.net.Socket.connect(Socket.java:861)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
at com.android.okhttp.Connection.connect(Connection.java:101)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)

第一次调用它时,它可以完美运行.但此后它会停止工作,并且可能会在一段时间后随机开始工作.

First time when it gets called it works perfectly. But it stops working after it and it may start working randomly after some time.

public class HTTPLoader {
    public static String loadContentFromURLGET(String urlString,List<String[]> getVars,Context context){
        int retry = 0;
        HttpURLConnection con=null;
        BufferedReader in = null;
        StringBuffer response=null;
        if (!isConnectingToInternet(context)){
            return "{'error':'No Internet connection!'}";
        }
        while (retry++<=RETRY_CNT) {
            try {
                String urlParameters = "";
                for (String[] var : getVars) {
                    urlParameters += var[0] + "=" + URLEncoder.encode(var[1], "UTF-8") + "&";
                }
                if (urlParameters.length() > 1) {
                    urlParameters = urlParameters.substring(0, urlParameters.length() - 1);
                }
                if (urlString.charAt(urlString.length() - 1) != '?') {
                    urlString += "&";
                }
                URL url = new URL(urlString + urlParameters);
                con = (HttpURLConnection) url.openConnection();
                con.setConnectTimeout(3000);
                con.setRequestMethod("GET");
                con.setRequestProperty("User-Agent", USER_AGENT);
                con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
                con.setDoInput(true);
                con.setDoOutput(true);
                int responseCode = con.getResponseCode();
                in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                retry = RETRY_CNT+1;
                break;
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
            }finally {
                if (in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (con!=null) {
                    con.disconnect();
                }
                in = null;
                con = null;
            }
        }
        if (response!=null)
            return new String(response);
        return "{'error':'No Internet connection!'}";
    }
}

正在从IntentService调用此loadContentFromURLGET

This loadContentFromURLGET is getting called from IntentService

public class ChatUtil extends IntentService{
    protected String loadAllChats(String date){
            String response = "";
            try {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                String email = sharedPreferences.getString(QuickstartPreferences.EMAIL, "");

            List<String[]> postVars = new ArrayList<>();
            postVars.add(new String[]{"getconversation", "yes"});
            postVars.add(new String[]{"user_id", email});
            postVars.add(new String[]{"last_date", date});
            String urlString = getString(R.string.get_conversation_url);
            response = HTTPLoader.loadContentFromURLGET(urlString, postVars,getApplicationContext());
            Log.i(TAG, response.toString());
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.has("error")) {
                //Toast.makeText(getApplicationContext(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                return jsonObject.getString("error");
            }
        }catch (JSONException e) {
        }
    }
protected void onHandleIntent(Intent intent) {
    String task = intent.getStringExtra(QuickstartPreferences.CURRENT_TASK);
    Intent nintent;
    String date = "";
String[] arr3 = new NewsDBUtil(getApplicationContext()).getLastChatEntry(null);
            if (arr3!=null)
                date = arr3[1];
            loadAllChats(date);
            nintent = new Intent(QuickstartPreferences.LOADING_ALL_CHAT);
            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(nintent);
           }
}

尝试在finally块中关闭和断开流.但是没有成功.

Tried closing and disconnecting stream in finally block. But no Success.

推荐答案

通过 setFixedLengthStreamingMode

我发现某些设备生成了不完整的http请求

I found some devices generated incomplete http request

因为服务器必须等到服务器或客户端超时

cause the server has to wait until either server or client timed out

您可以使用Wireshark分析问题

you can use wireshark to analyze the problem

这篇关于HttpURLConnection中的SocketTimeoutException在getResponseCode中随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆