Android的HttpPost崩溃,不能调试? [英] Android HttpPost Crash, Can't Debug?
问题描述
设备是Xoom的平板电脑。出于某种原因,我不能做HttpPost,当我尝试我不能捕获的错误。我尝试了一堆基本的例子,他们都折戟以同样的方式(见堆栈跟踪)。我会附上我的表现的情况下,我错过了许可。我知道,这是发生在 client.execute(岗位);
语句。该错误是不困,所有我得到的是什么,你在下面的堆栈跟踪看看。我已经尝试了一堆不同的方式,但我不能为我的生活跟真正的错误是什么。 POST数据似乎并不重要,同样的事情发生了简单的HTTPGET请求。在地球上我缺少什么?
问题阻止
{尝试
HttpClient的客户端=新DefaultHttpClient();
HttpPost后=新HttpPost(http://share1.iqperspective.com/test);
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(阿瓦尔,测试数据));
post.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
client.execute(岗位); }赶上(UnsupportedEncodingException E1){
Log.v(TAGcatch1);
}赶上(ClientProtocolException E){
Log.v(TAGcatch2);
}赶上(IOException异常五){
Log.v(TAGcatch3);
}
Mainifest
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.quisenberry.iqperspective
安卓版code =1
机器人:=的versionName1.0>
<采用-SDK安卓的minSdkVersion =11/>
<应用机器人:图标=@绘制/ img_icon机器人:标签=@字符串/ APP_NAME>
<活动机器人:名字=IQPerspective机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途><使用许可权的android:NAME =android.permission.CAMERA/>
<使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>
<使用特征的android:NAME =android.hardware.camera/>
<使用特征的android:NAME =android.hardware.camera.autofocus/>
< /清单>
堆栈
发[< 1 GT;主](吊(例外NetworkOnMainThreadException))
DefaultRequestDirector.execute(HttpHost,Htt的prequest,HttpContext的)路线:530
DefaultHttpClient(AbstractHttpClient).execute(HttpHost,Htt的prequest,HttpContext的)路线:555
DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest,HttpContext的)路线:487
DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest)线:465
IQPerspective $ 13.onClick(视图)行:294
按钮(查看).performClick()行:3100
查看$ PerformClick.run()行:11644
的ViewRoot(处理器).handleCallback(消息)线:587
的ViewRoot(处理器).dispatchMessage(消息)行:92
Looper.loop()行:126
ActivityThread.main(字符串[])行:3997
Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本机方法]
Method.invoke(对象,对象...)线:491
ZygoteInit $ MethodAndArgsCaller.run()行:841
ZygoteInit.main(字符串[])线:599
NativeStart.main(字符串[])行:不可用[本机方法]
我觉得异常( NetworkOnMainThreadException
)的名称告诉你这是怎么回事。您需要在非UI线程中执行您的网络code。请参阅文章无痛线程获取更多信息。
这是新的东西蜂窝。见异常的文档的更多信息。
Device is a Xoom Tablet. For some reason I can't do a HttpPost, when I try I can't trap the error. I tried a bunch of basic examples and they all crashed out the same way (see stack trace). I will attach my manifest in case I missed a permission. I know it's happening in the client.execute(post);
statement. The error is not trapped and all I get is what you see in the stack trace below. I have tried a bunch of different ways but I can't for the life of me tell what the real error is. The post data does not seem to matter, same thing happens with a simple HttpGet request. What on earth am I missing?
Block in question
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>
The Stack
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]
I think the name of the exception (NetworkOnMainThreadException
) tells you what's going on. You need to execute your networking code on a non-UI thread. See the article Painless Threading for more info.
This is something new to Honeycomb. See the docs for the exception for more info.
这篇关于Android的HttpPost崩溃,不能调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!