java.net.SocketException:sendto failed:ECONNRESET(由peer重置连接)

时间:2016-04-26 17:19:07

标签: android socketexception econnreset

我尝试使用此代码连接到服务器以上传图像。

    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("connection", "close");
        System.setProperty("http.keepAlive", "false");
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }
    } catch (IOException e){
        if (e.getMessage().indexOf("Connection reset by peer") > 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

我总是得到SocketException,就像上面一样。

04-26 18:53:26.091 13039-13092/kovacsdev.hu.facec W/System.err: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
04-26 18:53:26.092 13039-13039/kovacsdev.hu.facec I/SurfaceTextureClient: [STC::queueBuffer] (this:0x53cf5360) fps:45.86, dur:1002.96, max:57.77, min:1.67
04-26 18:53:26.095 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:550)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:519)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:511)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:231)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.RetryableOutputStream.writeToSocket(RetryableOutputStream.java:70)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:814)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:293)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:505)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.RequestHandler.sendPostRequest(RequestHandler.java:43)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.UploadUI$1UploadImage.doInBackground(UploadUI.java:108)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.UploadUI$1UploadImage.doInBackground(UploadUI.java:82)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.lang.Thread.run(Thread.java:838)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err: Caused by: libcore.io.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
04-26 18:53:26.099 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.Posix.sendtoBytes(Native Method)
04-26 18:53:26.099 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.Posix.sendto(Posix.java:151)
04-26 18:53:26.100 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
04-26 18:53:26.100 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:517)

UploadUI在第108行包含此代码段:

String result = rh.sendPostRequest(UPLOAD_URL,data);

其中rh代表RequestHandler。它应该在SQL数据库中发布图片的细节,但服务器端代码似乎有效。

1 个答案:

答案 0 :(得分:7)

你好我也面临同样的问题。现在解决使用下面的代码。我希望它也会帮助你...试试....

将以下代码放在onCreate()

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_documents);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

   //Statement or Controls Declaration or your code

}