Android: Can not send http post
I've been banging my head trying to figure out how to send a post method in Android. This is how my code look like:
public class HomeActivity extends Activity implements OnClickListener {
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
pub开发者_开发百科lic void onClick(View view) {
HttpPost httpMethod = new HttpPost("http://www.example.com/");
httpMethod.addHeader("Accept", "text/html");
httpMethod.addHeader("Content-Type", "application/xml");
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
String result = null;
try {
HttpResponse response = client.execute(httpMethod);
textView.setText(response.toString());
HttpEntity entity = response.getEntity();
Log.i(HomeActivity.class.toString(), result);
textView.setText("Invoked webservice");
} catch (IOException e) {
e.printStackTrace();
Log.e(HomeActivity.class.toString(), e.getMessage());
textView.setText("Something wrong:" + e.getMessage());
}
}
}
This is the exception that I get:
I/ARMAssembler( 59): generated scanline__00000177:03515104_00001001_00000000 [
91 ipp] (114 ins) at [0x334348:0x334510] in 1430659 ns
W/System.err( 272): java.net.UnknownHostException: www.example.com
W/System.err( 272): at java.net.InetAddress.lookupHostByName(InetAddress.jav
a:513)
W/System.err( 272): at java.net.InetAddress.getAllByNameImpl(InetAddress.jav
a:278)
W/System.err( 272): at java.net.InetAddress.getAllByName(InetAddress.java:24
2)
W/System.err( 272): at org.apache.http.impl.conn.DefaultClientConnectionOper
ator.openConnection(DefaultClientConnectionOperator.java:136)
W/System.err( 272): at org.apache.http.impl.conn.AbstractPoolEntry.open(Abst
ractPoolEntry.java:164)
W/System.err( 272): at org.apache.http.impl.conn.AbstractPooledConnAdapter.o
pen(AbstractPooledConnAdapter.java:119)
W/System.err( 272): at org.apache.http.impl.client.DefaultRequestDirector.ex
ecute(DefaultRequestDirector.java:348)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:555)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:487)
W/System.err( 272): at org.apache.http.impl.client.AbstractHttpClient.execut
e(AbstractHttpClient.java:465)
W/System.err( 272): at android.net.http.AndroidHttpClient.execute(AndroidHtt
pClient.java:243)
W/System.err( 272): at com.collaboapp.android.HomeActivity.onClick(HomeActiv
ity.java:152)
W/System.err( 272): at android.view.View.performClick(View.java:2408)
W/System.err( 272): at android.view.View$PerformClick.run(View.java:8816)
W/System.err( 272): at android.os.Handler.handleCallback(Handler.java:587)
W/System.err( 272): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 272): at android.os.Looper.loop(Looper.java:123)
W/System.err( 272): at android.app.ActivityThread.main(ActivityThread.java:4
627)
W/System.err( 272): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 272): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 272): at com.android.internal.os.ZygoteInit$MethodAndArgsCalle
r.run(ZygoteInit.java:868)
W/System.err( 272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.ja
va:626)
W/System.err( 272): at dalvik.system.NativeStart.main(Native Method)
E/class android.HomeActivity( 272): www.example.com
D/ThrottleService( 59): finally have imsi - retreiving data
D/ThrottleService( 59): onPollAlarm - roaming =false, read =0, written =0, new
total =0
D/SntpClient( 59): request time failed: java.net.SocketException: Address fami
ly not supported by protocol
What am I doing wrong here? Is there anything that I may need to configure from the Android emulator to get this working?
Thank you for your help.
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();
}
精彩评论