Android中的Http连接

时间:2010-04-06 07:20:09

标签: android http

我是android新手... 我想连接服务器。喜欢我想发送数据并通过Http Connection从服务器接收数据。 任何人都可以帮我如何做到这一点。 任何人都可以提供侧客户端和服务器端的示例。 提前谢谢......

2 个答案:

答案 0 :(得分:1)

我刚刚开始阅读关于Android的内容,但我会在这里投入两分钱。显然,Android使用Apache HTTPComponents库来完成你想要做的事情。您应该在这里查看HttpClient教程: http://hc.apache.org/

我希望有所帮助。

答案 1 :(得分:1)

使用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);
}
  

私人处理程序处理程序=新   处理程序(){            public void handleMessage(Message msg){

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

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

}   } };