Android: Can not send http post

匆匆过客 提交于 2019-11-30 10:31:55

it seems that AndroidHttpClient is responsible for that exception. Your example will work

  • if use set the 'INTERNET' persmission as suggested
  • replace 'AndroidHttpClient' with 'DefaultHttpClient'
  • remove the line 'Log.i(HomeActivity.class.toString(), result);' because result is null

It is not clear to me why this class does not work as excpected, maybe somebody could explain. This thread discussed the problem too but there is also no explaination why the code fails: http://groups.google.de/group/android-developers/browse_thread/thread/cc59efb9475ac557/81116369f2c6bd7a?hl=de&lnk=gst&q=This+thread+forbids+HTTP+requests#81116369f2c6bd7a.

You can't call the web from the UI thread so you don't halt the app's UI, this blog post explains it with an example app using AndroidHttpClient: Official Android Dev Blog.

Here is a quote:

...this is such a bad idea that the AndroidHttpClient does not allow itself to be started from the main thread. The above code will display "This thread forbids HTTP requests" error messages instead. Use the DefaultHttpClient instead if you really want to shoot yourself in the foot.

So if you really want to run this on UI thread (bad idea, from personal experience and that blog post) then use DefaultHttpClient.

Do you have uses-permission in the AndroidManifest.xml ?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Thank you , I found this, I had just enable the StrictMode

 public void onCreate() {
 if (DEVELOPER_MODE) {
     StrictMode.enableDefaults();
 }
 super.onCreate();

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!