public class MainActivity extends Activity {
// 全局
NewsData newsData;
private Button btn1, btn2;
private ListView list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取事件源
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
list = (ListView) findViewById(R.id.list);
// 适配器
adapter = new MyAdapter();
list.setAdapter(adapter);
// 点击事件
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "这是HttpClient", 0).show();
adapter.notifyDataSetChanged();
init();
}
});
// 点击事件
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "这是HttpURLConnection", 0)
.show();
adapter.notifyDataSetChanged();
init();
}
});
}
/**
*
*/
protected void init() {
// 异步处理
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... arg0) {
// 获取数据
String result = initdata();
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// 解析json串
Gson gson = new Gson();
newsData = gson.fromJson(result, NewsData.class);
// 刷新适配器
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}.execute("");
}
protected String initdata() {
String result = "";
try {
// url
URL url = new URL(
"https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// code
int code = connection.getResponseCode();
// 判断
if (code == 200) {
// 获取数据
InputStream is = connection.getInputStream();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
stream.write(b, 0, len);
}
result = new String(stream.toByteArray());
return result;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
// 适配器
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
if (newsData != null) {
return newsData.getNewslist().size();
}
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return newsData.getNewslist().get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method
// 实例化
final AAA aaa;
if (arg1 == null) {
aaa = new AAA();
// 获取布局
arg1 = LayoutInflater.from(MainActivity.this).inflate(
R.layout.list, null);
aaa.ima = (ImageView) arg1.findViewById(R.id.ima);
aaa.text1 = (TextView) arg1.findViewById(R.id.text1);
aaa.text2 = (TextView) arg1.findViewById(R.id.text2);
arg1.setTag(aaa);
} else {
aaa = (AAA) arg1.getTag();
}
// 更改数据
aaa.text1.setText(newsData.getNewslist().get(arg0).getTitle());
aaa.text2.setText(newsData.getNewslist().get(arg0).getCtime());
new Thread() {
public void run() {
final Bitmap bitmap = getima(newsData.getNewslist()
.get(arg0).getPicUrl());
runOnUiThread(new Runnable() {
public void run() {
aaa.ima.setImageBitmap(bitmap);
}
});
};
}.start();
return arg1;
}
// aaa类
class AAA {
TextView text1, text2;
ImageView ima;
}
}
// 获取图片方法
public Bitmap getima(String path) {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
NewsData类
public class NewsData {
// 定义私有属性
private String code;
private String msg;
private List newslist;
News类
public class News {
// 定义私有属性
private String ctime;
private String description;
private String picUrl;
private String title;
private String url;