I try to use this code to connect to server for uploading an image.
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();
}
I always get SocketException
, like above.
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
contains this code snippet on line 108:
String result = rh.sendPostRequest(UPLOAD_URL,data);
where rh
is stand for RequestHandler
. It should post picture's details in a SQL database, but server side code seems working.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…