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();

相關文章
相關標籤/搜索