tags:

views:

304

answers:

2

hello folks, 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...

+1  A: 

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.

Mike Williamson
Hi Sleepycat,thnks a lot for this file.Can you provide something same like this for android with samples.
remish
Hey Remish, like I said I am just reading about this now. The book I am reading is "Beginning Android 2" from Apress. Its a great book.
Mike Williamson
A: 

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;

}   } };
Alexandru Mos
Hi Alexandru Mos,thnks for the reply.Can you tell me what is handler here " handler.sendEmptyMessage(0);" because it is giving error.
remish
Uhhh... sorry, I forget about the handler. Take a look here, http://commons.apache.org/io/description.html
Alexandru Mos
Can you give me your email id so that i can contact you directly,if you dont have any prob
remish
Hi i did like this bt google.com is not opening..public class WebToolKit extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
remish
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) {
remish
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; } } }; }
remish
bt its nt opening the..where i am wrong..
remish
Hi Alexandru u dere???
remish