工具类
public class Utils {
public static InputStream getInputStreamFromPath(String path) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
int requestCode = httpResponse.getStatusLine().getStatusCode();
if (requestCode == 200) {
return httpResponse.getEntity().getContent();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String getJsonFromInputStream(InputStream inputStream) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
try {
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
String json=byteArrayOutputStream.toString();
return json;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static ArrayList<Map<String, Object>> getListFromString(String json) {
ArrayList<Map<String,Object>> arrayList=new ArrayList<>();
try {
JSONObject jsonObject=new JSONObject(json);
JSONArray jsonArray=jsonObject.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++){
Map<String,Object> map=new HashMap<>();
map.put("header",jsonArray.getJSONObject(i).getString("image"));
map.put("title",jsonArray.getJSONObject(i).getString("title"));
map.put("pubDate",jsonArray.getJSONObject(i).getString("pubDate"));
map.put("URL",jsonArray.getJSONObject(i).getString("url"));
arrayList.add(map);
}
return arrayList;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
主程序
public class MainActivity extends AppCompatActivity {
private String path;
private ListView listView;
private ArrayList<Map<String,Object>> list=new ArrayList<>();
private int pageNo=1;
private int pageSize=20;
private int size;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView_list);
path= "http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo="+pageNo+"&pageSize="+pageSize+"&serialIds=2143,3404&v=4.0.0";
getData(path);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState==0&&size==pageNo*pageSize){
pageNo++;
path= "http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo="+pageNo+"&pageSize="+pageSize+"&serialIds=2143,3404&v=4.0.0";
getData(path);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
size=firstVisibleItem+visibleItemCount;
}
});
}
class MyAsyncTask extends AsyncTask<String,Void,ArrayList<Map<String,Object>>>{
@Override
protected ArrayList<Map<String, Object>> doInBackground(String... params) {
ArrayList<Map<String,Object>> sub;
InputStream inputStream= Utils.getInputStreamFromPath(params[0]);
String json=Utils.getJsonFromInputStream(inputStream);
sub=Utils.getListFromString(json);
list.addAll(sub);
return null;
}
@Override
protected void onPostExecute(ArrayList<Map<String, Object>> maps) {
if(myAdapter==null) {
myAdapter=new MyAdapter(MainActivity.this, list);
listView.setAdapter(myAdapter);
}else {
myAdapter.notifyDataSetChanged();
}
}
}
public void getData(String path){
new MyAsyncTask().execute(path);
}
class MyAdapter extends BaseAdapter{
private LayoutInflater layoutInflater;
ArrayList<Map<String,Object>> arrayList;
public MyAdapter(Context context,ArrayList<Map<String,Object>> list){
layoutInflater=LayoutInflater.from(context);
arrayList=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class VoidHolder{
ImageView imageView;
TextView title;
TextView pubDate;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final VoidHolder voidHolder;
final Uri uri;
if(convertView==null){
convertView=layoutInflater.inflate(R.layout.item,null);
voidHolder=new VoidHolder();
voidHolder.imageView= (ImageView) convertView.findViewById(R.id.img_header);
voidHolder.title= (TextView) convertView.findViewById(R.id.textView_title);
voidHolder.pubDate= (TextView) convertView.findViewById(R.id.textView_pubDate);
convertView.setTag(voidHolder);
}else{
voidHolder= (VoidHolder) convertView.getTag();
}
voidHolder.imageView.setImageResource(R.mipmap.ic_launcher);
final String url=arrayList.get(position).get("header").toString();
voidHolder.imageView.setTag(url);
new Thread(new Runnable() {
Bitmap bitmap;
@Override
public void run() {
final String localUrl=voidHolder.imageView.getTag().toString();
InputStream inputStream=Utils.getInputStreamFromPath(url);
bitmap=BitmapFactory.decodeStream(inputStream);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(localUrl!= null &&localUrl.equals(url)) {
voidHolder.imageView.setImageBitmap(bitmap);
}
}
});
}
}).start();
uri= Uri.parse(arrayList.get(position).get("URL").toString());
voidHolder.title.setText(arrayList.get(position).get("title").toString());
voidHolder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW,uri));
}
});
voidHolder.pubDate.setText(arrayList.get(position).get("pubDate").toString());
return convertView;
}
}
}