首先一 ,依赖jar包
二,添加联网权限:
创建一个适配器类 继承BaseAdapter
数据Bean类
MainActivity 中代码:
···
package com.example.wangye.androidxmlc_asynchttptest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
ListView listView;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
listView = (ListView) findViewById(R.id.listview);
getData();
}
public void getData(){
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://apicloud.mob.com/wx/article/search?key=26b2b13b4b440&cid=1",
new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Gson gson = new Gson();
WxBean wxBean = gson.fromJson(new String(responseBody,0,responseBody.length),WxBean.class);
adapter = new MyAdapter(MainActivity.this,wxBean);
listView.setAdapter(adapter);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
}
···
适配器类 :
···
package com.example.wangye.androidxmlc_asynchttptest;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.CacheMode;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;
/**
- Created by wangye on 2018/8/10.
*/
public class MyAdapter extends BaseAdapter{
Context context;
WxBean wxBean;
public MyAdapter(Context context,WxBean wxBean){
this.context = context;
this.wxBean = wxBean;
NoHttp.initialize(context);
}
@Override
public int getCount() {
return wxBean.getResult().getList().size();
}
@Override
public Object getItem(int i) {
return wxBean.getResult().getList().get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(context,R.layout.adapter,null);
final ImageView ig = view.findViewById(R.id.imageView);
Request<Bitmap> request = NoHttp.createImageRequest(
wxBean.getResult().getList().get(i).getThumbnails(),
RequestMethod.GET);
request.setCacheMode(CacheMode.NONE_CACHE_REQUEST_NETWORK);
RequestQueue queue = NoHttp.newRequestQueue();
queue.add(i, request, new OnResponseListener<Bitmap>() {
@Override
public void onStart(int what) {
}
@Override
public void onSucceed(int what, Response<Bitmap> response) {
ig.setImageBitmap(response.get());
}
@Override
public void onFailed(int what, Response<Bitmap> response) {
}
@Override
public void onFinish(int what) {
}
});
TextView tx = view.findViewById(R.id.textView);
tx.setText(wxBean.getResult().getList().get(i).getTitle());
return view;
}
}
···
效果图: