HttpClient和HttpURLConnection網路請求資料
阿新 • • 發佈:2019-02-18
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;