HttpClient的get用法

httpClient(执行时都需要放进线程中):

try {

String path="路径";

HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(path);

HttpResponse response = httpClient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();


if(code==200){

  InputStream is = response.getEntity().getContent();
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  int len;
  byte[] by=new byte[1024];
  while((len=is.read(by))!=-1){
    bos.write(by,0,len);
  }
  String json=bos.toString();

     //解析gson字符串
  Gson gson=new Gson();
  History history = gson.fromJson(json, History.class);
  List<ArrayHis> his=history.array;

  return his;
}
} catch (Exception e) {

e.printStackTrace();
}

posted @ 2016-04-14 15:18  投个硬币吧  阅读(230)  评论(0)    收藏  举报