remish
remish

Reputation: 1

Http Connection in Android

i am new to android... I want to connect with server .Like i want to sent data and recieve data from server through Http Connection.. Can anyone help me how to do this. Can anyone provide me the sample of both side-client as well as server side. Thanks in advance...

Upvotes: 0

Views: 4785

Answers (2)

Alexandru Mos
Alexandru Mos

Reputation: 67

Quick working example using Apache HTTPComponents:

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com");
ResponseHandler<String> responseHandler = new BasicResponseHandler();

try {
 String reqData = httpclient.execute(httpget, responseHandler).toString();
 httpclient.getConnectionManager().shutdown();
 handler.sendEmptyMessage(0);
} catch (ClientProtocolException e) {           
 handler.sendEmptyMessage(1);
} catch (IOException e) {
 handler.sendEmptyMessage(1);
}

private Handler handler = new Handler() { public void handleMessage(Message msg) {

switch (msg.what) {
  case 0: {
    // all ok, process data
  } break;

  case 1: {
    // show some errors
  } break;

}   } };

Upvotes: 1

mikewilliamson
mikewilliamson

Reputation: 24783

I'm just starting reading about Android, but I'll throw in my two cents here. Apparently Android uses Apache HTTPComponents library to do what you are looking to do. You should check out the HttpClient tutorials here: http://hc.apache.org/

I hope that helps.

Upvotes: 1

Related Questions