【问题标题】:Getting java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) [duplicate]获取java.net.SocketException:recvfrom失败:ECONNRESET(对等方重置连接)[重复]
【发布时间】:2014-03-09 01:46:59
【问题描述】:

我是安卓新手。 通过 HttpClient 更新时出现错误,例如 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

public static String newupdateProf(String memid, String gender,
                String pdata, String dob, String pimg) throws IOException,
                JSONException {
            // System.setProperty("http.keepAlive", "false");
            final HttpClient httpClient = new DefaultHttpClient();
            final HttpPost httpost = new HttpPost(websrv.trim() + "/newUpdtProf");

            JSONStringer img = new JSONStringer().object().key("prof").object()
                    .key("memid").value(memid.trim()).key("gender")
                    .value(gender.trim()).key("pdata").value(pdata.trim())
                    .key("dob").value(dob.trim()).key("pimg").value(pimg.trim())
                    .endObject().endObject();
            StringEntity se = new StringEntity(img.toString());
            httpost.setEntity(se);
            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");

            HttpResponse httpResponse = httpClient.execute(httpost);

            HttpEntity httpEntity = httpResponse.getEntity();

            InputStream stream = httpEntity.getContent();
            String result = convertStreamToString(stream);
            return result.trim();

        }

请帮助我。 我也试过了

System.setProperty("http.keepAlive", "false"); 

但它不起作用。

【问题讨论】:

  • 答案是服务器的错——它必须在每次请求后关闭连接。可能是android保留了一个连接池并使用旧的连接池或类似的东西

标签: java android


【解决方案1】:

在发出任何 HTTP 请求之前将 http.keepAlive 系统属性设置为 false。

System.setProperty("http.keepAlive", "false");

【讨论】:

  • 它对我有用。但他的答案是什么解释?
  • 为我工作!这种解决方案在某些情况下是否有可能适得其反?
猜你喜欢
  • 2013-01-05
  • 2017-02-22
  • 2012-10-13
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2011-12-30
相关资源
最近更新 更多