Я просто обновляю свой Android с 2.3.4 до 4.0.3, но у меня возникает проблема, когда я хочу опубликовать файл (около 2 МБ) на php-сервере с помощью метода http post.Но похоже, что Android 4.0.3 не работает, а Android 2.3.4 работает хорошо.
Ниже приведена часть моего кода 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 ().
Пожалуйста, помогите мне в этом ...
Большое вам спасибо!