我尝试使用此代码连接到服务器以上传图像.
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.6704-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.access0(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.UploadUIUploadImage.doInBackground(UploadUI.java:108)04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err: at kovacsdev.hu.facec.UploadUIUploadImage.doInBackground(UploadUI.java:82)04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err: at androID.os.AsyncTask.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.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数据库中发布图片的细节,但服务器端代码似乎有效.
解决方法:
嗨我也面临同样的问题.现在解决使用下面的代码.我希望它也会帮助你..试试….
将此代码放在onCreate()中
@OverrIDeprotected 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}
总结 以上是内存溢出为你收集整理的android – java.net.SocketException:sendto failed:ECONNRESET(由peer重置连接)全部内容,希望文章能够帮你解决android – java.net.SocketException:sendto failed:ECONNRESET(由peer重置连接)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)