Jason 键值对 网络请求

  1 package com.example.administrator.myapplication.activity;
  2 
  3 import android.os.Bundle;
  4 import android.os.Handler;
  5 import android.os.Message;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.webkit.WebView;
  9 import android.widget.Button;
 10 import android.widget.Toast;
 11 
 12 import com.example.administrator.myapplication.R;
 13 import com.example.administrator.myapplication.entity.SportNews;
 14 
 15 import org.json.JSONArray;
 16 import org.json.JSONException;
 17 import org.json.JSONObject;
 18 
 19 import java.io.IOException;
 20 import java.io.InputStream;
 21 import java.net.HttpURLConnection;
 22 import java.net.MalformedURLException;
 23 import java.net.URL;
 24 import java.util.ArrayList;
 25 import java.util.List;
 26 
 27 public class JsonActivity extends AppCompatActivity {
 28     WebView jsonWebView;
 29     Button jsonBtn;
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_json);
 34         jsonWebView = (WebView) findViewById(R.id.jsonWebView);
 35         jsonBtn = (Button) findViewById(R.id.pressJson);
 36         jsonBtn.setOnClickListener(new View.OnClickListener() {
 37             @Override
 38             public void onClick(View v) {
 39                 new Thread(new Runnable() {
 40                     @Override
 41                     public void run() {
 42                         PressJson();
 43                     }
 44                 }).start();
 45             }
 46         });
 47     }
 48     public void PressJson(){
 49         InputStream is = null;
 50         HttpURLConnection httpURLConnection = null;
 51         StringBuilder sb = new StringBuilder();
 52         try {
 53             URL url = new URL("http://apis.baidu.com/txapi/weixin/wxhot?num=10&page=1&word=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0");
 54             httpURLConnection = (HttpURLConnection) url.openConnection();
 55             httpURLConnection.setReadTimeout(5*1000);
 56             httpURLConnection.setConnectTimeout(5*1000);
 57             httpURLConnection.setRequestProperty("apikey","58218dcc8845195b277082c3a357f481");
 58             httpURLConnection.connect();
 59             if (httpURLConnection.getResponseCode() == httpURLConnection.HTTP_OK){
 60                 is = httpURLConnection.getInputStream();
 61                 byte[] bytes = new byte[1024];
 62                 int i = 0;
 63                 while ((i = is.read(bytes)) != -1){
 64                     sb.append(new String(bytes,0,i,"utf-8"));
 65                 }
 66                 is.close();
 67             }
 68         } catch (MalformedURLException e) {
 69             e.printStackTrace();
 70         } catch (IOException e) {
 71             e.printStackTrace();
 72         }finally {
 73             if (httpURLConnection!= null){
 74                 httpURLConnection.disconnect();
 75             }
 76         }
 77 
 78         Message message = handler.obtainMessage(1,sb.toString());
 79         handler.sendMessage(message);
 80     }
 81     Handler handler = new Handler(){
 82         @Override
 83         public void handleMessage(Message msg) {
 84             super.handleMessage(msg);
 85             if (msg != null && msg.what == 1){
 86                 String s = (String) msg.obj;
 87                 jsonWebView.getSettings().setDefaultTextEncodingName("utf-8");
 88                 jsonWebView.getSettings().setJavaScriptEnabled(true);
 89                 jsonWebView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
 90                 //调用解析字符串的方法
 91                 List<SportNews> list = pressJsonToList(s);
 92                 SportNews sn = list.get(0);
 93                 Toast.makeText(getApplication(),sn.getTitle()+" "+sn.getDescription()+" "+sn.getCtime(),Toast.LENGTH_SHORT).show();
 94             }
 95         }
 96     };
 97     private List<SportNews> pressJsonToList(String s){
 98         List<SportNews> list = new ArrayList<SportNews>();
 99         try {
100             //将传过来的字符串先解析为JSON对象
101             JSONObject obj = new JSONObject(s);
102             String code = obj.getString("code");
103             String msg = obj.getString("msg");
104             JSONArray array = obj.getJSONArray("newslist");
105             for (int i = 0 ; i < array.length() ; i++){
106                 JSONObject obj2 = array.getJSONObject(i);
107                 SportNews news = new SportNews(
108                         obj2.getString("ctime"),
109                         obj2.getString("title"),
110                         obj2.getString("description"),
111                         obj2.getString("picUrl"),
112                         obj2.getString("url")
113                 );
114                 list.add(news);
115             }
116         } catch (JSONException e) {
117             e.printStackTrace();
118         }
119         return list;
120     }
121 }
原文地址:https://www.cnblogs.com/xiaolei121/p/5893433.html