httppost的连接:NetworkOnMainThreadException

时间:2014-01-04 23:56:32

标签: android

我正在尝试将png文件上传到服务器。问题是什么? 我认为连接存在问题,但地址是正确的。这是一个php URL。那么,问题是什么?它不上传图片。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
BMUpload.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair( "id", String.valueOf(((App)Global).m_iUserID) ));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(adrupload);
try
{
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httpclient.execute(httppost);
}catch(Exception e)
{
    e.printStackTrace();
    return;
}

错误日志:

01-05 01:50:11.591: W/System.err(11415): android.os.NetworkOnMainThreadException
01-05 01:50:11.596: W/System.err(11415):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-05 01:50:11.596: W/System.err(11415):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-05 01:50:11.596: W/System.err(11415):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-05 01:50:11.596: W/System.err(11415):    at com.todogram.Settings.UpdateAvatar(Settings.java:351)
01-05 01:50:11.596: W/System.err(11415):    at com.todogram.Settings.onActivityResult(Settings.java:139)
01-05 01:50:11.596: W/System.err(11415):    at android.app.Activity.dispatchActivityResult(Activity.java:5563)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.access$1200(ActivityThread.java:159)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-05 01:50:11.596: W/System.err(11415):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 01:50:11.596: W/System.err(11415):    at android.os.Looper.loop(Looper.java:137)
01-05 01:50:11.596: W/System.err(11415):    at android.app.ActivityThread.main(ActivityThread.java:5419)
01-05 01:50:11.596: W/System.err(11415):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 01:50:11.596: W/System.err(11415):    at java.lang.reflect.Method.invoke(Method.java:525)
01-05 01:50:11.596: W/System.err(11415):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-05 01:50:11.596: W/System.err(11415):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-05 01:50:11.601: W/System.err(11415):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

Android操作系统阻止任何应用程序使用网络相关进程(例如,加载站点,从服务器发送或接收数据等)在主线程中执行。这背后的逻辑是网络进程往往很慢(如果你的连接速度慢,那么更多)并且在主线程上放置这样的进程将大大减慢程序的速度 - 而慢程序则是一个用户体验不佳的程序。

你必须将它放在一个线程中并在onCreate()函数或按下按钮的函数中使用该线程。

This link should get you started with threads and file upload.