Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid 4.0.3 HTTP POST file to PHP server failed
    primarykey
    data
    text
    <p>I am just upgrading my Android from 2.3.4 to 4.0.3, but find a problem when I want to post a file (around 2MB) to a php server through the http post method. But it looks like Android 4.0.3 does not work while Android 2.3.4 works well.</p> <p>The following is part of my Android code to post a file:</p> <pre><code>private boolean postFile(String urlstr, String localpath, String uploadname) { HttpURLConnection conn = null; DataOutputStream os = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize, bytesUploaded = 0; byte[] buffer; int maxBufferSize = 2*1024*1024; try { FileInputStream fis = new FileInputStream(new File(localpath) ); URL url = new URL(urlstr); conn = (HttpURLConnection) url.openConnection(); conn.setChunkedStreamingMode(maxBufferSize); // POST settings. conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary); os = new DataOutputStream(conn.getOutputStream()); os.writeBytes(twoHyphens + boundary + lineEnd); os.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uploadname +"\"" + lineEnd); os.writeBytes(lineEnd); bytesAvailable = fis.available(); System.out.println("available: " + String.valueOf(bytesAvailable)); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; prog = 0; bytesRead = fis.read(buffer, 0, bufferSize); bytesUploaded += bytesRead; while (bytesRead &gt; 0) { prog = bytesUploaded/bytesAvailable; os.write(buffer, 0, bufferSize); bytesAvailable = fis.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fis.read(buffer, 0, bufferSize); bytesUploaded += bytesRead; } System.out.println("uploaded: "+String.valueOf(bytesUploaded)); os.writeBytes(lineEnd); os.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) conn.setConnectTimeout(2000); // allow 2 seconds timeout. int rcode = conn.getResponseCode(); if (rcode == 200) Toast.makeText(getApplicationContext(), "Success!!", Toast.LENGTH_LONG).show(); else Toast.makeText(getApplicationContext(), "Failed!!", Toast.LENGTH_LONG).show(); //String rmsg = conn.getResponseMessage(); fis.close(); os.flush(); os.close(); return rcode == 200; } catch (Exception ex) { ex.printStackTrace(); return false; } } </code></pre> <p>Then I get the following exception:</p> <pre><code>02-02 22:38:34.626: W/System.err(5547): android.os.NetworkOnMainThreadException 02-02 22:38:34.631: W/System.err(5547): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 02-02 22:38:34.631: W/System.err(5547): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 02-02 22:38:34.631: W/System.err(5547): at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 02-02 22:38:34.636: W/System.err(5547): at libcore.io.IoBridge.connect(IoBridge.java:112) 02-02 22:38:34.641: W/System.err(5547): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 02-02 22:38:34.641: W/System.err(5547): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 02-02 22:38:34.641: W/System.err(5547): at java.net.Socket.connect(Socket.java:842) 02-02 22:38:34.641: W/System.err(5547): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:77) 02-02 22:38:34.646: W/System.err(5547): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50) 02-02 22:38:34.646: W/System.err(5547): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 02-02 22:38:34.646: W/System.err(5547): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 02-02 22:38:34.646: W/System.err(5547): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 02-02 22:38:34.651: W/System.err(5547): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 02-02 22:38:34.651: W/System.err(5547): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 02-02 22:38:34.651: W/System.err(5547): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 02-02 22:38:34.651: W/System.err(5547): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 02-02 22:38:34.656: W/System.err(5547): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 02-02 22:38:34.656: W/System.err(5547): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188) 02-02 22:38:34.656: W/System.err(5547): at edu.socialsense.SocialsenseService.postFile(SocialsenseService.java:324) 02-02 22:38:34.661: W/System.err(5547): at edu.socialsense.SocialsenseService.report(SocialsenseService.java:285) 02-02 22:38:34.661: W/System.err(5547): at edu.socialsense.SocialsenseService.access$7(SocialsenseService.java:279) 02-02 22:38:34.661: W/System.err(5547): at edu.socialsense.SocialsenseService$1.onReceive(SocialsenseService.java:79) 02-02 22:38:34.661: W/System.err(5547): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728) 02-02 22:38:34.661: W/System.err(5547): at android.os.Handler.handleCallback(Handler.java:605) 02-02 22:38:34.666: W/System.err(5547): at android.os.Handler.dispatchMessage(Handler.java:92) 02-02 22:38:34.666: W/System.err(5547): at android.os.Looper.loop(Looper.java:137) 02-02 22:38:34.666: W/System.err(5547): at android.app.ActivityThread.main(ActivityThread.java:4507) 02-02 22:38:34.666: W/System.err(5547): at java.lang.reflect.Method.invokeNative(Native Method) 02-02 22:38:34.671: W/System.err(5547): at java.lang.reflect.Method.invoke(Method.java:511) 02-02 22:38:34.671: W/System.err(5547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 02-02 22:38:34.671: W/System.err(5547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 02-02 22:38:34.671: W/System.err(5547): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>When I follow the code from step to step, I found the exception happened when the url connection tried to connect, say conn.connect() or conn.getOutputStream().</p> <p>Please help me on this...</p> <p>Thank you so much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload