Android: Can not send http post

Posted by jpartogi on Stack Overflow See other posts from Stack Overflow or by jpartogi
Published on 2010-06-13T11:22:13Z Indexed on 2010/06/13 11:32 UTC
Read the original article Hit count: 438

Filed under:
|
|

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());
        }
    }
}

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about android