布局
实现代码
/** * 使用异步任务下载网络数据
* 使用gson解析
* 注:有时间看看我讲的原生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必须大写
// 连接网络 connection.connect();
// 判断是否连接成功 if (connection.getResponseCode() == 200) {
// 获取来自网络的输入流
// 使用缓冲字符输入流
// 字节流转字符流
BufferedReader br = null;
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// 最终接收的json字符串
String json = "";
// 使用缓冲流读取到的每一行数据
String line = "";
while ((line = br.readLine()) != null) {
json += line.trim();
}
// json数据产生完毕,该解析了
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, "下载错误", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override protected void onPostExecute(ListitemList) {
super.onPostExecute(itemList);
// 可以使用RecyclerView或ListView展示数据
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;
}
}
}
}