views:

860

answers:

2

Hi all,

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
    public 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.

A: 

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

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
qrtt1
W/dalvikvm( 261): threadid=1: thread exiting with uncaught exception (group=0x4001d800)E/AndroidRuntime( 261): FATAL EXCEPTION: mainE/AndroidRuntime( 261): java.lang.RuntimeException: This thread forbids HTTP requests
jpartogi
+1  A: 

Hi, 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&amp;lnk=gst&amp;q=This+thread+forbids+HTTP+requests#81116369f2c6bd7a.

Kewl. That fixed it. Thanks.
jpartogi