我只是將我的Android從2.3.4升級到4.0.3,但是當我想要將文件(大約2MB)發佈到php服務器時發現問題http post方法。但看起來Android 4.0.3不起作用,而Android 2.3.4運行良好。Android 4.0.3 HTTP POST文件到PHP服務器失敗
以下是我的Android代碼來發布文件的一部分:
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 > 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;
}
}
然後我得到以下異常:
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.<init>(HttpConnection.java:77)
02-02 22:38:34.646: W/System.err(5547): at libcore.net.http.HttpConnection.<init>(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)
當我按照步驟代碼一步,我發現當url連接嘗試連接時發生異常,比如conn.connect()或conn.getOutputStream()。
請幫助我...
非常感謝!
謝謝@BalaK Taduri,但它看起來像代碼仍然不正確時,我刪除此行。我收到上面列出的同樣的錯誤。 – 2012-02-10 16:26:14