Android HttpPost崩溃,无法调试?
问题描述:
设备是Xoom平板电脑。出于某种原因,我不能做HttpPost,当我尝试时,我无法捕捉到错误。我尝试了一些基本的例子,它们都以相同的方式崩溃(请参阅堆栈跟踪)。如果我错过了许可,我会附上我的清单。我知道这发生在client.execute(post);
声明中。错误没有被捕获,我得到的只是你在下面的堆栈跟踪中看到的内容。我尝试了很多不同的方式,但是我不能告诉我真实的错误是什么。发布数据似乎并不重要,同样的事情发生在一个简单的HttpGet请求。我究竟在想什么?Android HttpPost崩溃,无法调试?
块有问题
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://share1.iqperspective.com/test");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("avar", "test data"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
client.execute(post);
}catch (UnsupportedEncodingException e1) {
Log.v(TAG, "catch1");
}catch (ClientProtocolException e) {
Log.v(TAG, "catch2");
}catch (IOException e) {
Log.v(TAG, "catch3");
}
Mainifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quisenberry.iqperspective"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/img_icon" android:label="@string/app_name">
<activity android:name="IQPerspective" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
</manifest>
堆栈
Thread [<1> main] (Suspended (exception NetworkOnMainThreadException))
DefaultRequestDirector.execute(HttpHost, HttpRequest, HttpContext) line: 530
DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, HttpContext) line: 555
DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, HttpContext) line: 487
DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest) line: 465
IQPerspective$13.onClick(View) line: 294
Button(View).performClick() line: 3100
View$PerformClick.run() line: 11644
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 126
ActivityThread.main(String[]) line: 3997
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 491
ZygoteInit$MethodAndArgsCaller.run() line: 841
ZygoteInit.main(String[]) line: 599
NativeStart.main(String[]) line: not available [native method]
答
我觉得异常(NetworkOnMainThreadException
)的名字告诉你这是怎么回事。您需要在非UI线程上执行您的网络代码。有关更多信息,请参阅文章Painless Threading。
这是Honeycomb的一些新功能。有关更多信息,请参阅docs for the exception。
谢谢先生,我完全看过了明显的错误。我在线上发现的任何示例中都没有看到线程,只是将它误认为主线程上的一个通用异常。我的头发留下了什么谢谢你。 – 2011-05-12 18:03:36