zoukankan      html  css  js  c++  java
  • 网络解析json

    public class myActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }


    public void buooo(View v) throws Exception{

    new Thread(){
    public void run() {

    try {
    getdate();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }.start();
    }
    private void getdate() throws Exception {
    // TODO Auto-generated method stub
    URL url=new URL(
    "http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee");
    HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.connect();
    StringBuffer stringBuffer= new StringBuffer();
    final StringBuilder sb1 = new StringBuilder();
    String data = null;
    InputStream inputStream=httpURLConnection.getInputStream();
    BufferedReader be=new BufferedReader(new InputStreamReader(inputStream));
    while ((data = be.readLine()) != null) {
    stringBuffer.append(data);
    }
    JSONObject jsonObject=new JSONObject(stringBuffer.toString());
    jsonObject=jsonObject.getJSONObject("result");
    JSONArray array=jsonObject.getJSONArray("data");
    jsonObject = (JSONObject)array.get(1);
    sb1.append("标题:" + jsonObject.get("title") + " ");
    sb1.append("时间:" + jsonObject.get("date") + " ");
    sb1.append("类型:" + jsonObject.get("category") + " ");
    sb1.append("作者:" + jsonObject.get("author_name") + " ");
    runOnUiThread(new Runnable() {

    @Override
    public void run() {
    // 就相当于在UI线程中运行一样
    Toast.makeText(getApplicationContext(), sb1.toString(), 1)
    .show();
    }
    });
    inputStream.close();
    be.close();

    };
    }

  • 相关阅读:
    typescript
    pyqt5窗口跳转
    pyqt5 列表内添加按钮
    C#窗体最大化,其他控件调整
    C#禁止程序重复打开
    C#添加 mysql.data.dll
    宝塔一键ssl
    宝塔Linux面板 使用阿里云OSS备份数据
    CentOS7使用firewalld打开关闭防火墙与端口
    使用babel编译es6
  • 原文地址:https://www.cnblogs.com/YFeiY7/p/6413014.html
Copyright © 2011-2022 走看看