网络数据解析

来源:互联网 发布:特斯拉models知乎 编辑:程序博客网 时间:2024/04/27 18:57

这里写图片描述


核心代码:

JSONObject jsonObject = new JSONObject(msg);int status = jsonObject.getInt("status");String msg2 = jsonObject.getString("msg");Log.e("TAG", status + "  " + msg2);JSONObject data = jsonObject.getJSONObject("data");String title = data.getString("title");String author = data.getString("author"); String content = data.getString("content");Log.e("TAG", "标题:" + title + ",作者:" + author + ",内容:" + content);
package com.studio.imoocdemo;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import org.json.JSONException;import org.json.JSONObject;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class DetailActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_detail);        initData();    }    public void initData() {        //HttpUrlConnection        /**         * 1.实例化一个url对象         * 2.获取HttpUrlConnection对象         * 3.设置请求连接属性         * 4.获取相应码,判断是否连接成功         * 5.读取输入流并解析         */        //参数:你要访问的接口地址        new Thread() {            @Override            public void run() {                super.run();                try {                    URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();                    httpURLConnection.setRequestMethod("GET");                    httpURLConnection.setReadTimeout(6000);                    //获取相应码                    if (httpURLConnection.getResponseCode() == 200) {                        //获取输入流                        InputStream inputStream = httpURLConnection.getInputStream();                        byte[] bytes = new byte[1024 * 512];                        int len = 0;                        //建立缓存流,保存所读取的字节数组                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();                        while ((len = inputStream.read(bytes)) > -1) {                            byteArrayOutputStream.write(bytes, 0, len);                        }                        String msg = byteArrayOutputStream.toString();                        Log.e("TAG", msg);                        //JSON数据的解析:                        JSONObject jsonObject = new JSONObject(msg);                        //JSONArray jsonArray = new JSONArray();                        int status = jsonObject.getInt("status");                        String msg2 = jsonObject.getString("msg");                        Log.e("TAG", status + "  " + msg2);                        JSONObject data = jsonObject.getJSONObject("data");                        String title = data.getString("title");                        String author = data.getString("author");                        String content = data.getString("content");                        Log.e("TAG", "标题:" + title + ",作者:" + author + ",内容:" + content);                        }                } catch (MalformedURLException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                } catch (JSONException e) {                    e.printStackTrace();                }            }        }.start();    }}
原创粉丝点击