关闭

异步交互AsyncTask

65人阅读 评论(0) 收藏 举报
//异步请求
class MyAsyncTask extends AsyncTask<String, String, String>{


private String string;


@Override
protected String doInBackground(String... params) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
string = EntityUtils.toString(entity,"gbk");

// System.out.println(string);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return string;
}

}






public void httpGet(){

try {

MyAsyncTask my=new MyAsyncTask();
String str = my.execute(path).get();
// System.out.println(str);




Type type=new TypeToken<List<Bean>>(){}.getType();




Gson gson=new Gson();
List<Bean> bean=gson.fromJson(str, type);
System.out.println(bean.size());

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3844次
    • 积分:284
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条