android訪問web的輔助類
2011/12/26 8:49:52
public class HttpUtil {
public static final String BASE_URL="http://192.168.1.100/android.ashx";
//獲得GET請求request
public static HttpGet getHttpGet(String url){
  HttpGet request=new HttpGet(url);
  return request;
}
//獲得Post請求對象request
public static HttpPost getHttpPost(String url){
  HttpPost request=new HttpPost(url);
  return request;
}
//根據(jù)請求獲得響應(yīng)對象response
public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{
  HttpResponse response=new DefaultHttpClient().execute(request);
  return response;
}
//根據(jù)請求獲得響應(yīng)對象response
public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{
  HttpResponse response=new DefaultHttpClient().execute(request);
  return response;
}
//發(fā)送Post請求,獲得響應(yīng)查詢結(jié)果
public static String queryStringForPost(String url){
  //根據(jù)url獲得HttpPost對象
  HttpPost request=HttpUtil.getHttpPost(url);
  String result=null;
 
  try {
  //獲得響應(yīng)對象
  HttpResponse response = HttpUtil.getHttpResponse(request);
  //判斷是否請求成功
  if(response.getStatusLine().getStatusCode()==200){
    result=EntityUtils.toString(response.getEntity());
  }
  } catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  }
  return result;
}

public static String queryStringForPost(HttpPost request){
  String result=null;
  try {
  HttpResponse response=HttpUtil.getHttpResponse(request);
  if(response.getStatusLine().getStatusCode()==200){
    result=EntityUtils.toString(response.getEntity());
  }
  } catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  }
  return result;
}
public static String queryStringForGet(String url){
  HttpGet request=HttpUtil.getHttpGet(url);
  String result=null;
  try {
  HttpResponse response=HttpUtil.getHttpResponse(request);
  if(response.getStatusLine().getStatusCode()==200){
    result=EntityUtils.toString(response.getEntity());
  }
  } catch (ClientProtocolException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  result="網(wǎng)絡(luò)異常!";
  }
  return result;
}

}

返回列表
返回首頁 主站蜘蛛池模板: 亚洲高清成人欧美动作片| 国产精品嫩草影院av| 久久久久夜夜夜精品国产| 欧美国产小视频| 国产综合在线观看| 一本色道久久综合亚洲精品高清| 激情五月综合网| 口国产成人高清在线播放| 韩国理论三级在线观看视频| 国产精品亚韩精品无码a在线| 久久99国产精品久久99| 最近中文字幕国语免费完整| 亚洲欧美中文日韩二区一区| 草草影院ccyy国产日本欧美| 国产欧美日韩另类va在线 | 色婷婷综合久久久久中文字幕| 天天射天天干天天插| 久久精品无码免费不卡| 特黄特黄一级高清免费大片| 台湾swag视频在线观看| 野外亲子乱子伦视频丶久草资源| 国精品在亚洲_欧美| 久久久久久一区国产精品| 最近中文字幕mv在线视频www| 亚洲成人aaa| 波多野结衣有一部紧身裙女教师| 免费黄色福利视频| 黄色a视频在线观看| 壮汉紫黑粗大好深用力| 久久久久亚洲AV无码麻豆| 日韩美女一级视频| 亚洲一级片在线观看| 第一章岳婿之战厨房沈浩| 四虎国产精品永久地址99| 视频一区二区在线观看| 国产精品自产拍在线观看| 中文天堂最新版www官网在线| 机巴太粗太硬弄死你| 亚洲成av人片在线观看无码不卡| 特级毛片a级毛片在线播放www| 免费毛片在线播放|