手机版
你好,游客 登录 注册
背景:
阅读新闻

Java中的HTTP通信

[日期:2016-08-17] 来源:Linux社区  作者:mstk [字体:大 中 小]

在Android中,HTTP通信可以用Volley,在Java中不能使用Volley,只能使用DefaultHttpClient,HttpPost和HttpResponse。

    /*
    * 向服务器发送数据,并接受返回的数据
    */
    public static String send2Server(String url, List <NameValuePair> params){
        String res = null;
       
        // 建立HTTPPost连接
        HttpPost httpRequest = new HttpPost(url);
        try
        {
            // 发出HTTP Request
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            // 取得HTTP Response
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
            res = EntityUtils.toString(httpResponse.getEntity());
        }
        catch (Exception e){
            e.printStackTrace();
        }
       
        return res;
    }

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134359.htm

linux
相关资讯       Java中的HTTP通信  HTTP通信 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款