package com.bwie.asyntask;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.bwie.asyntask.School;
import com.bwie.asyntask.Schools;
import com.bwie.asyntask.School.Result;
import com.google.gson.Gson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv_list;
private String uri = "http://apis.juhe.cn/goodbook/catalog?key=cca134699ab46a8322a322c8c011449a";
private ArrayList<Schools> lists=new ArrayList<Schools>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list=(ListView)findViewById(R.id.lv_list);
// 得到MyAsyncTask对象
MyAsyncTask myAsyncTask=new MyAsyncTask();
try {
// 执行任务(耗时操作),参数:需要执行的任务,比如:联网,就是服务器的地址,该参数被doInBackground方法接收
//.get()方法获取doInBackground方法的返回值
@SuppressWarnings("unused")
String s = myAsyncTask.execute(uri).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
//泛型一:doInBackground方法接收的参数类型(耗时操作)
//泛型二:onProgressUpdate方法接收的参数类型(更新进度)
//泛型三:doInBackground方法返回值的参数类型,onPostExecute方法参数类型
class MyAsyncTask extends AsyncTask<String, Integer, String>{
// 运行在主线程,在doInBackground方法执行之前执行
@Override
protected void onPreExecute() {
Toast.makeText(MainActivity.this, "联网马上开始 ", 0).show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
Log.i("http_", "联网正在进行");
// 发布请求数据的进度,参数被onProgressUpdate方法接收
publishProgress(0);
publishProgress(25);
publishProgress(50);
publishProgress(100);
// 取参数列表,数组角标为0的参数,也就是myAsyncTask.execute传递的参数
String string = params[0];
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(string);
String s = null;
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode==200) {
HttpEntity entity = httpResponse.getEntity();
s = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
// 运行在主线程,做进度提示,接收 publishProgress 发送的进度值
@Override
protected void onProgressUpdate(Integer... values) {
Toast.makeText(MainActivity.this, "当前进度 " + values[0], 0).show();
super.onProgressUpdate(values);
}
// 运行在主线程,在doInBackground方法执行之后执行,并且接收doInBackground方法的返回值
@SuppressLint("ShowToast")
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "联网结束 " + result, 0).show();
Log.i("http_", result);
Gson gson=new Gson();
School school = gson.fromJson(result, School.class);
ArrayList<Result> results = school.result;
for (Result result2 : results) {
String id = result2.id;
String catalog = result2.catalog;
lists.add(new Schools(id, catalog));
}
adapter=new MyAdapter(lists, MainActivity.this);
lv_list.setAdapter(adapter);
}
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.bwie.asyntask.School;
import com.bwie.asyntask.Schools;
import com.bwie.asyntask.School.Result;
import com.google.gson.Gson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv_list;
private String uri = "http://apis.juhe.cn/goodbook/catalog?key=cca134699ab46a8322a322c8c011449a";
private ArrayList<Schools> lists=new ArrayList<Schools>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list=(ListView)findViewById(R.id.lv_list);
// 得到MyAsyncTask对象
MyAsyncTask myAsyncTask=new MyAsyncTask();
try {
// 执行任务(耗时操作),参数:需要执行的任务,比如:联网,就是服务器的地址,该参数被doInBackground方法接收
//.get()方法获取doInBackground方法的返回值
@SuppressWarnings("unused")
String s = myAsyncTask.execute(uri).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
//泛型一:doInBackground方法接收的参数类型(耗时操作)
//泛型二:onProgressUpdate方法接收的参数类型(更新进度)
//泛型三:doInBackground方法返回值的参数类型,onPostExecute方法参数类型
class MyAsyncTask extends AsyncTask<String, Integer, String>{
// 运行在主线程,在doInBackground方法执行之前执行
@Override
protected void onPreExecute() {
Toast.makeText(MainActivity.this, "联网马上开始 ", 0).show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
Log.i("http_", "联网正在进行");
// 发布请求数据的进度,参数被onProgressUpdate方法接收
publishProgress(0);
publishProgress(25);
publishProgress(50);
publishProgress(100);
// 取参数列表,数组角标为0的参数,也就是myAsyncTask.execute传递的参数
String string = params[0];
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(string);
String s = null;
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode==200) {
HttpEntity entity = httpResponse.getEntity();
s = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
// 运行在主线程,做进度提示,接收 publishProgress 发送的进度值
@Override
protected void onProgressUpdate(Integer... values) {
Toast.makeText(MainActivity.this, "当前进度 " + values[0], 0).show();
super.onProgressUpdate(values);
}
// 运行在主线程,在doInBackground方法执行之后执行,并且接收doInBackground方法的返回值
@SuppressLint("ShowToast")
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "联网结束 " + result, 0).show();
Log.i("http_", result);
Gson gson=new Gson();
School school = gson.fromJson(result, School.class);
ArrayList<Result> results = school.result;
for (Result result2 : results) {
String id = result2.id;
String catalog = result2.catalog;
lists.add(new Schools(id, catalog));
}
adapter=new MyAdapter(lists, MainActivity.this);
lv_list.setAdapter(adapter);
}
}
}