布局
實(shí)現(xiàn)代碼
/** * 使用異步任務(wù)下載網(wǎng)絡(luò)數(shù)據(jù)
* 使用gson解析
* 注:有時(shí)間看看我講的原生json解析
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? public static final String KAI_YAN_URL = "http://baobab.kaiyanapp.com/api/v4/tabs/selected?udid=11111&vc=168&vn=3.3.1&deviceModel=Huawei%36&first_channel=eyepetizer_baidu_market&last_channel=eyepetizer_baidu_market&system_version_code=20"; protected Button mGetNetworkDataBtn;
? protected Button mGetPicBtn;
? protected ImageView mImageView;
?? @Override protected void onCreate(Bundle savedInstanceState) {
?????? super.onCreate(savedInstanceState);
?????? super.setContentView(R.layout.activity_main);
?????? initView();
? }
? @Override public void onClick(View view) {
????? if (view.getId() == R.id.get_network_data_btn) {
?????????? getDatas();
?????? } else if (view.getId() == R.id.get_pic_btn) {
?????????? getPicture();
????? }
? }
?private void getDatas() {
?????? MyTask task = new MyTask();
????? ?task.execute(KAI_YAN_URL);
?? }
? private void getPicture() {
????? String imgUrl = "http://img.kaiyanapp.com/1699f3946ccd281230a1d887d169439f.pngimageMogr2/quality/60/format/jpg";
?????? Picasso .with(this) .load(imgUrl) .into(mImageView);
? }
? private void initView() {
?????? mGetNetworkDataBtn = (Button) findViewById(R.id.get_network_data_btn);???
?????? mGetNetworkDataBtn.setOnClickListener(MainActivity.this);
?????? mGetPicBtn = (Button) findViewById(R.id.get_pic_btn);
?????? mGetPicBtn.setOnClickListener(MainActivity.this);
?????? mImageView = (ImageView) findViewById(R.id.image_view);
?? }
?? private class MyTask extends AsyncTask> {
????? @Override protected ListdoInBackground(String... params) {
?????????? String requestUrl = params[0];
?????????? // 使用HTTPUrlConnection
?????????? try {
????????????? URL url = new URL(requestUrl);
????????????? HttpURLConnection connection = (HttpURLConnection) url.openConnection();
????????????? // 配置connection
????????????? connection.setRequestMethod("GET");? // GET或POST必須大寫(xiě)
????????????? // 連接網(wǎng)絡(luò) connection.connect();
????????????? // 判斷是否連接成功 if (connection.getResponseCode() == 200) {
????????????? // 獲取來(lái)自網(wǎng)絡(luò)的輸入流
????????????? // 使用緩沖字符輸入流
????????????? // 字節(jié)流轉(zhuǎn)字符流
????????????? BufferedReader br = null;
????????????? br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
????????????? // 最終接收的json字符串
???????????? String json = "";
???????????? // 使用緩沖流讀取到的每一行數(shù)據(jù)
???????????? String line = "";
???????????? while ((line = br.readLine()) != null) {
????????????????? json += line.trim();
????????????? }
?????????????// json數(shù)據(jù)產(chǎn)生完畢,該解析了
???????????? Gson gson = new Gson();
???????????? KaiYan kaiYan = gson.fromJson(json, KaiYan.class);
???????????? ListitemList = kaiYan.getItemList();
???????????? return itemList;
????????? }
??? } catch (Exception e) {
?????????? e.printStackTrace();
????????? Toast.makeText(MainActivity.this, "下載錯(cuò)誤", Toast.LENGTH_SHORT).show();
?????? }
?????????? return null;
?????? }
?????? @Override protected void onPostExecute(ListitemList) {
??????????? super.onPostExecute(itemList);
???????????? // 可以使用RecyclerView或ListView展示數(shù)據(jù)
??????????? for (KaiYan.ItemBean itemBean : itemList) {
??????????? Log.d("1507", "type: " + itemBean.getType() +
??????????? ", title: " + itemBean.getData().getTitle() +
????????????", url" + itemBean.getData().getPlayUrl());
??????????? }
????? }
? }
}
Bean
package net.bwie.network.bean;
import java.util.List;public class KaiYan {
?? private ListitemList; public ListgetItemList() {
????? return itemList;
?? }
? public void setItemList(ListitemList) {
this.itemList = itemList;
}
public static class ItemBean {
private String type = "";
private DataBean data;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class DataBean {
private String playUrl = "";
private String title = "";
public String getPlayUrl() {
return playUrl;
}
public void setPlayUrl(String playUrl) {
this.playUrl = playUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}