布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="net.bwie.network.MainActivity"> 10 11 <Button 12 android:id="@+id/get_network_data_btn" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="下载网络数据"/> 16 17 <Button 18 android:id="@+id/get_pic_btn" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="获取网络图片"/> 22 23 <ImageView 24 android:id="@+id/image_view" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:src="@mipmap/ic_launcher"/> 28 29 </LinearLayout>
Activity:
1 /** 2 * 使用异步任务下载网络数据 3 * 使用gson解析 4 * 注:有时间看看我讲的原生json解析 5 */ 6 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 7 8 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"; 9 10 protected Button mGetNetworkDataBtn; 11 protected Button mGetPicBtn; 12 protected ImageView mImageView; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 super.setContentView(R.layout.activity_main); 18 initView(); 19 } 20 21 @Override 22 public void onClick(View view) { 23 if (view.getId() == R.id.get_network_data_btn) { 24 getDatas(); 25 } else if (view.getId() == R.id.get_pic_btn) { 26 getPicture(); 27 } 28 } 29 30 private void getDatas() { 31 MyTask task = new MyTask(); 32 task.execute(KAI_YAN_URL); 33 } 34 35 private void getPicture() { 36 String imgUrl = "http://img.kaiyanapp.com/1699f3946ccd281230a1d887d169439f.png?imageMogr2/quality/60/format/jpg"; 37 38 Picasso 39 .with(this) 40 .load(imgUrl) 41 .into(mImageView); 42 } 43 44 private void initView() { 45 mGetNetworkDataBtn = (Button) findViewById(R.id.get_network_data_btn); 46 mGetNetworkDataBtn.setOnClickListener(MainActivity.this); 47 mGetPicBtn = (Button) findViewById(R.id.get_pic_btn); 48 mGetPicBtn.setOnClickListener(MainActivity.this); 49 mImageView = (ImageView) findViewById(R.id.image_view); 50 } 51 52 private class MyTask extends AsyncTask<String, Void, List<KaiYan.ItemBean>> { 53 54 @Override 55 protected List<KaiYan.ItemBean> doInBackground(String... params) { 56 String requestUrl = params[0]; 57 // 使用HTTPUrlConnection 58 try { 59 URL url = new URL(requestUrl); 60 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 61 // 配置connection 62 connection.setRequestMethod("GET");// GET或POST必须大写 63 // 连接网络 64 connection.connect(); 65 // 判断是否连接成功 66 if (connection.getResponseCode() == 200) { 67 // 获取来自网络的输入流 68 // 使用缓冲字符输入流 69 // 字节流转字符流 70 BufferedReader br = null; 71 br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 72 73 // 最终接收的json字符串 74 String json = ""; 75 // 使用缓冲流读取到的每一行数据 76 String line = ""; 77 78 while ((line = br.readLine()) != null) { 79 json += line.trim(); 80 } 81 // json数据产生完毕,该解析了 82 Gson gson = new Gson(); 83 KaiYan kaiYan = gson.fromJson(json, KaiYan.class); 84 List<KaiYan.ItemBean> itemList = kaiYan.getItemList(); 85 return itemList; 86 87 } 88 } catch (Exception e) { 89 e.printStackTrace(); 90 Toast.makeText(MainActivity.this, "下载错误", Toast.LENGTH_SHORT).show(); 91 } 92 93 94 return null; 95 } 96 97 @Override 98 protected void onPostExecute(List<KaiYan.ItemBean> itemList) { 99 super.onPostExecute(itemList); 100 101 // 可以使用RecyclerView或ListView展示数据 102 for (KaiYan.ItemBean itemBean : itemList) { 103 Log.d("1507", "type: " + itemBean.getType() + 104 ", title: " + itemBean.getData().getTitle() + 105 ", url" + itemBean.getData().getPlayUrl()); 106 } 107 } 108 } 109 110 }
Bean:
1 package net.bwie.network.bean; 2 3 import java.util.List; 4 5 public class KaiYan { 6 7 private List<ItemBean> itemList; 8 9 public List<ItemBean> getItemList() { 10 return itemList; 11 } 12 13 public void setItemList(List<ItemBean> itemList) { 14 this.itemList = itemList; 15 } 16 17 public static class ItemBean { 18 19 private String type = ""; 20 private DataBean data; 21 22 public String getType() { 23 return type; 24 } 25 26 public void setType(String type) { 27 this.type = type; 28 } 29 30 public DataBean getData() { 31 return data; 32 } 33 34 public void setData(DataBean data) { 35 this.data = data; 36 } 37 38 public static class DataBean { 39 40 private String playUrl = ""; 41 private String title = ""; 42 43 public String getPlayUrl() { 44 return playUrl; 45 } 46 47 public void setPlayUrl(String playUrl) { 48 this.playUrl = playUrl; 49 } 50 51 public String getTitle() { 52 return title; 53 } 54 55 public void setTitle(String title) { 56 this.title = title; 57 } 58 } 59 60 } 61 62 }
权限: 1 <uses-permission android:name="android.permission.INTERNET"/>