MainActivity里的方法参数看一下代码:
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public String urlString="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
String urlBitmap="https://img-my.youkuaiyun.com/uploads/201407/26/1406383265_8550.jpg";
private ListView li;
private List<Javabing.DataBean> list;
private MybaseAdapter mb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li = (ListView) findViewById(R.id.li);
new Myasy().execute(urlString);
list = new ArrayList<Javabing.DataBean>();
Myasy my=new Myasy();
mb = new MybaseAdapter();
li.setAdapter(mb);
my.execute(urlString);
}
private class Myasy extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
String str=strings[0];
String json = Netutil.getJson(str);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
Javabing ja = gson.fromJson(s, Javabing.class);
List<Javabing.DataBean> data = ja.getData();
list.addAll(data);
mb.notifyDataSetChanged();
}
}
class MybaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View vi = view.inflate(MainActivity.this, R.layout.two, null);
TextView te = vi.findViewById(R.id.te);
te.setText(list.get(i).getNews_summary());
ImageView im = vi.findViewById(R.id.im);
new MyAsynctask(im).execute(urlBitmap);
return vi;
}
}
private class MyAsynctask extends AsyncTask<String,Void,Bitmap>{
private ImageView im;
public MyAsynctask(ImageView im) {
this.im=im;
}
@Override
protected Bitmap doInBackground(String... strings) {
return Netutil.getNetBitmap(strings[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
im.setImageBitmap(bitmap);
}
}
工具包Utils里的方法,如一下代码:
public static String getJson(String urlString){
try {
URL url=new URL(urlString);
HttpURLConnection ur = (HttpURLConnection) url.openConnection();
int responseCode = ur.getResponseCode();
if (responseCode==200){
InputStream inpu = ur.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(inpu));
StringBuffer sb=new StringBuffer();
String line=null;
while ((line=br.readLine())!=null){
sb.append(line);
}
return sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static Bitmap getNetBitmap(String urlString){
try {
URL url=new URL(urlString);
HttpURLConnection ur = (HttpURLConnection) url.openConnection();
int re = ur.getResponseCode();
if (re==200){
InputStream inputStream = ur.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
然后自己在封装一个Javabing,用来解析Gson串,
中间适配器也需要XML文件,手动建造即可。
public static String getJson(String urlString){ try { URL url=new URL(urlString); HttpURLConnection ur = (HttpURLConnection) url.openConnection(); int responseCode = ur.getResponseCode(); if (responseCode==200){ InputStream inpu = ur.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(inpu)); StringBuffer sb=new StringBuffer(); String line=null; while ((line=br.readLine())!=null){ sb.append(line); } return sb.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static Bitmap getNetBitmap(String urlString){ try { URL url=new URL(urlString); HttpURLConnection ur = (HttpURLConnection) url.openConnection(); int re = ur.getResponseCode(); if (re==200){ InputStream inputStream = ur.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }然后自己在封装一个Javabing,用来解析Gson串,
中间适配器也需要XML文件,手动建造即可。