【问题标题】:Http Connection in AndroidAndroid中的Http连接
【发布时间】:2010-04-06 07:20:09
【问题描述】:

我是安卓新手... 我想与服务器连接。就像我想通过 Http 连接从服务器发送数据和接收数据一样。 谁能帮助我如何做到这一点。 谁能给我提供客户端和服务器端的样本。 提前谢谢...

【问题讨论】:

    标签: android http


    【解决方案1】:

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

    希望对你有帮助。

    【讨论】:

    • 嗨,Sleepycat,非常感谢这个文件。你能为带有示例的 android 提供类似的东西吗?
    • 嘿 Remish,就像我说的那样,我现在只是在读这个。我正在阅读的书是 Apress 的“Beginning Android 2”。这是一本好书。
    【解决方案2】:

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

    私有处理程序处理程序 = 新 处理程序(){ 公共无效句柄消息(消息消息){

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

    【讨论】:

    • 嗨,Alexandru Mos,谢谢回复。你能告诉我这里的处理程序是什么“handler.sendEmptyMessage(0);”因为它给出了错误。
    • 呃……对不起,我忘记了处理程序。看这里,commons.apache.org/io/description.html
    • 你能给我你的电子邮件ID,如果你没有任何问题,我可以直接联系你
    • 嗨,我喜欢这个 bt google.com is not opening.. public class WebToolKit extends Activity { /** 在首次创建活动时调用。 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
    • HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("google.com"); ResponseHandler 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) {
    猜你喜欢
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2013-09-06
    相关资源
    最近更新 更多