我有Socket Timeout Exception,但我的数据也在服务器上提交

时间:2015-03-24 11:24:59

标签: android json sockets rest http

我有一个问题,http发布请求我已设置超时和套接字超时异常我在服务器上提交的数据成功如何我可以限制数据提交到服务器上我的代码正在跟随

try{HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    AppSettings.SERVICE_URL.POST_NEW_REGISTRATION);
            HttpConnectionParams.setConnectionTimeout(
                    httpClient.getParams(), 5000);
            HttpConnectionParams.setSoTimeout(httpClient.getParams(), 5000);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("agent_id", SharedPrefrence.getUserID());
            httpPost.setEntity(new StringEntity(jsonObject.toString(),
                    "UTF-8"));
            // Set up the header types needed to properly transfer JSON
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept-Encoding", "application/json");
            // Execute POST
            //int getConnectionTimeout (HttpParams params);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity responseEntity = httpResponse.getEntity();
            if (responseEntity != null) {
                response = EntityUtils.toString(responseEntity);
            } else {
                response = "{'success':'FALSE'}";
            }
        } catch (ClientProtocolException e) {
            response = "{'success':'FALSE'}";
            progressDialog.cancel();
        } catch (IOException e) {
            response = "{'success':'FALSE','message':'Connection Time Out'}";
            Log.d("Ex", e.toString());
            Log.e("Ex", e.toString());
            progressDialog.cancel();
        } catch (JSONException e) {
            response = "{'success':'FALSE','message':'JSON Parse Error'}";
            progressDialog.cancel();
            e.printStackTrace();
        } catch (Exception e) {
            response = "{'success':'FALSE'}";
            progressDialog.cancel();
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:1)

我认为即使服务器成功接收到数据,您也会收到套接字超时异常?

如果是这样,我想你可能想看看你为套接字超时设置的值 - 这在代码中似乎是500毫秒,这很短。很可能服务器端的所有内容都工作正常,但响应在500毫秒内无法到达客户端。

典型的默认值在6-10秒范围内,但它确实取决于解决方案,因此您可能需要进行实验。此博客文章为不同的方案提供了一些默认值,可能是一个有用的参考:

答案 1 :(得分:0)

可能你的API调用需要超过500毫秒,增加它并重试。