获取java.net.SocketException:recvfrom失败:ECONNRESET(对等方重置连接)

3

我是Android新手。 当通过HttpClient更新时,遇到错误:java.net.SocketException: recvfrom失败:ECONNRESET(对等方重置连接)。

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"); 

但是它没有起作用。

1
答案是服务器的问题 - 每个请求后它必须关闭连接。可能是Android保留了一组连接并使用旧连接或类似的东西。 - Jitesh Upadhyay
1个回答

8
在发出任何HTTP请求之前,将http.keepAlive系统属性设置为false。
System.setProperty("http.keepAlive", "false");

7
它对我起作用了。但他的答案是什么解释? - Bhavit S. Sengar
1
对我有用!这个解决方案在某些情况下会出现反作用的可能吗? - Rahul Rastogi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接