//异步请求
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();
}
}
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();
}
}