joepetrakovich
joepetrakovich

Reputation: 1366

How to quickly test an HTTP post response in an Android emulator

Let's say I want to perform a quick HTTP post to a website within my android app.

If I were to set up the http client and send the post all in the onCreate method of the main screen (inside the UI thread) just for testing purposes, what would I use to quickly see what the response is to the post?

I tried something quick and dirty below inside the onCreate method when the app first opens and nothing really happens, is there something more I need to do/obviously my toast idea is just bad...

  HttpClient httpClient = new DefaultHttpClient();
  HttpPost post = new HttpPost("http://forecast.weather.gov/zipcity.php?inputstring=04419");

  HttpResponse httpResp;

  try {
   httpResp = httpClient.execute(post);
   StatusLine status = httpResp.getStatusLine();

      Toast.makeText(getApplicationContext(), (CharSequence) status, Toast.LENGTH_SHORT).show();
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

Upvotes: 0

Views: 2674

Answers (1)

Ollie C
Ollie C

Reputation: 28509

LogCat is your friend. It gives you a view of what's happening inside the device. Then in your code add statements like this:

Log.i("MyClassName","HTTP response is " + httpResponseString);

It's common to create a class member variable called "TAG" and pass that as the first parameter, e.g.:

private String TAG = "MyClassName";
....

Log.i(TAG,"HTTP response is " + httpResponseString);

You can view LogCat using DDMS (Dalvik Debug Monitor), an IDE (Eclipse, IntelliJ IDEA, etc), and there's loads of LogCat apps in the Android Market that you can run on the phone.

Upvotes: 1

Related Questions