httpclient发送网络请求

小程序,微信公众号开发,短信发送等等各类网络请求的发送,均可以使用HttpClient来发送。json

 

初始化HttpClient对象小程序

HttpClient httpClient = new HttpClient();微信

定义请求地址url网络

String url = "";post

经过get/post方式提交编码

HttpGet httpGet = new HttpGet(url);url

HttpPost httpPost = new HttpPost(url);对象

经过execute方法发送请求  返回一个httpresponse对象  抛出异常开发

HttpResopnse response = httpClient.execute(httpGet);字符串

HttpResopnse response = httpClient.execute(httpPost);

从response获取结果

HttpEntity entity = response.getEntity();

判断结果

if(entity!=null){

    获取返回的字符串  设置编码格式

    String result = EntityUtils.toString(entity,"UTF-8");

    //将字符串转换成json

    JSONObject object = JSONObject.fromObject(result);

}

这里的fromObject方法是net.sf.json的包

关闭链接  释放连接

httpGet.releaseConnection();

相关文章
相关标签/搜索