主界面代码如下,仅仅为了记录Asynctask的使用。业务代码写在Activity里面肯定不好的。最好使用MVP模式来实现。
publicclass MainActivity extendsActivity {
privatestatic String url = "http://www.imooc.com/api/teacher?type=2&num=100";
privateListView myListView;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView) findViewById(R.id.myListView);
newMyAsynctask().execute(url);
}
/**
* 实现网络的异步访问
*
* @author Administrator
*
*/
classMyAsynctask extendsAsyncTask<String, Void, List<NewsBean>> {
@Override
protectedList<NewsBean> doInBackground(String... params) {
returngetNetWorkDatas(params[0]);
}
@Override
protectedvoid onPostExecute(List<NewsBean> result) {
super.onPostExecute(result);
NewsAdapter adapter = newNewsAdapter(result,
getApplicationContext());
myListView.setAdapter(adapter);
}
}
/**
* 获取制定的 json 数据转换成我们需要的 NewsBean 对象
*
* @param url
* @return
*/
publicList<NewsBean> getNetWorkDatas(String url) {
List<NewsBean> myList = newArrayList<>();
try{
String jsonString = getJsonString(newURL(url).openStream());
Log.i("sdaf", jsonString.toString());
JSONObject jsonObject;
jsonObject = newJSONObject(jsonString);
JSONArray array = jsonObject.getJSONArray("data");
for(inti = 0; i < array.length(); i++) {
NewsBean bean = newNewsBean();
JSONObject obj = array.getJSONObject(i);
bean.image_icon = obj.getString("picSmall");
bean.title = obj.getString("name");
bean.content = obj.getString("description");
myList.add(bean);
}
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(JSONException e) {
e.printStackTrace();
}
returnmyList;
}
/**
* 获取返回的String 数据
*
* @param is
* @return
*/
publicString getJsonString(InputStream is) {
String result = "";
InputStreamReader isr;
try{
String line = "";
isr = newInputStreamReader(is, "UTF-8");
BufferedReader br = newBufferedReader(isr);
while((line = br.readLine()) != null) {
result += line;
}
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
returnresult;
}
}
publicclassNewsAdapter extendsBaseAdapter {
privateList<NewsBean> datas;
privateContext context;
publicNewsAdapter(List<NewsBean> datas, Context context) {
super();
this.datas = datas;
this.context = context;
}
@Override
publicintgetCount() {
returndatas.size();
}
@Override
publicObject getItem(intposition) {
returndatas.get(position);
}
@Override
publiclonggetItemId(intposition) {
returnposition;
}
@Override
publicView getView(intposition, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
viewHolder = newViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.item_class,null);
viewHolder.title = (TextView) convertView
.findViewById(R.id.tv_title);
viewHolder.content = (TextView) convertView
.findViewById(R.id.tv_content);
viewHolder.iv_icon = (ImageView) convertView
.findViewById(R.id.iv_icon);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
NewsBean bean = datas.get(position);
viewHolder.iv_icon.setImageResource(R.drawable.ic_launcher);
String url = bean.image_icon;
viewHolder.iv_icon.setTag(url);
// new ImageLoader().showImageByThread(viewHolder.iv_icon,
// bean.image_icon);
newImageLoader().showImageByAsyncTask(viewHolder.iv_icon, url);
viewHolder.title.setText(bean.title);
viewHolder.content.setText(bean.content);
returnconvertView;
}
classViewHolder {
publicTextView title;
publicTextView content;
publicImageView iv_icon;
}
}
publicclassImageLoader {
privateString murl;
privateImageView imageview;
privateHandler handler = newHandler() {
publicvoidhandleMessage(android.os.Message msg) {
super.handleMessage(msg);
if(imageview.getTag().equals(murl)) {
imageview.setImageBitmap((Bitmap) msg.obj);
}
};
};
publicvoidshowImageByThread(ImageView imageView, finalString url) {
imageview = imageView;
murl = url;
newThread() {
@Override
publicvoidrun() {
super.run();
Bitmap bitmap = getBitMapByUrl(url);
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);
};
}.start();
}
publicBitmap getBitMapByUrl(String url) {
Bitmap bitmap;
InputStream is = null;
try{
URL ul = newURL(url);
HttpURLConnection connection = (HttpURLConnection) ul
.openConnection();
is = newBufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
Thread.sleep(1000);
returnbitmap;
}catch(MalformedURLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}finally{
try{
is.close();
}catch(IOException e) {
e.printStackTrace();
}
}
returnnull;
}
publicvoidshowImageByAsyncTask(ImageView imageView, String url) {
newNewsAsyncTask(imageView,url).execute(url);
}
privateclassNewsAsyncTask extendsAsyncTask<String, Void, Bitmap> {
privateImageView mimageview;
privateString mUrl;
publicNewsAsyncTask(ImageView imageview,String url) {
mimageview = imageview;
mUrl= url;
}
@Override
protectedBitmap doInBackground(String... arg0) {
returngetBitMapByUrl(arg0[0]);
}
@Override
protectedvoidonPostExecute(Bitmap result) {
super.onPostExecute(result);
if(mimageview.getTag().equals(mUrl)) {
mimageview .setImageBitmap(result);
}
}
}
}
publicclassNewsBean {
publicString title;
publicString content;
publicString image_icon;
}