开发者

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


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.


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;

}   } };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜