在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;
    }

 

相关文章:

  • 2022-02-07
  • 2021-05-25
  • 2021-12-28
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-15
  • 2021-09-26
猜你喜欢
  • 2021-04-22
  • 2021-12-29
  • 2021-11-22
  • 2021-06-27
  • 2022-12-23
  • 2021-04-22
相关资源
相似解决方案