• 网络载入数据和解析JSON格式数据案例之空气质量监測应用


    一、创建一个新的项目
    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/reload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Reload" />
    
        <TextView
            android:id="@+id/tvPmData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:text="TextView" />
    
    </LinearLayout>

    二、MainActivity.java

    package com.xuliugen.airpm;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private TextView tvPmData;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tvPmData = (TextView) this.findViewById(R.id.tvPmData);
    
            findViewById(R.id.reload).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    reloadData();
                }
            });
    
        }
    
        private void reloadData() {
            tvPmData.setText("Loading...");
            new AsyncTask<Void, Void, String>() {
    
                @Override
                protected String doInBackground(Void... params) {
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(new URL(
                                        "http://aqicn.org/publishingdata/json")
                                        .openStream(), "utf-8"));
    
                        String line = null;
                        StringBuffer contentBuffer = new StringBuffer();
    
                        while ((line = reader.readLine()) != null) {
                            contentBuffer.append(line);
                        }
                        reader.close();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(String result) {
                    if (result != null) {
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            JSONObject firstJsonObject = jsonArray.getJSONObject(0);
    
    
                            JSONArray pollutantsArray = firstJsonObject.getJSONArray("pollutants");
                            JSONObject firstpollutants = pollutantsArray.getJSONObject(0);
    
                            tvPmData.setText(String.format("%s %s:%f",
                                    firstJsonObject.getString("cityName"),
                                    firstJsonObject.getString("localName"),
                                    firstpollutants.getDouble("value")));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
            }.execute();
        }
    }
    

    三、还有一个JSON的案例代码

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String jsonStr = "{"name":"jikexueyuan","age":2,"arr":{1,2,3,4,5,6,"jike"}}";
    
            try {
                JSONObject jsonObject = new JSONObject(jsonStr);
                String name = jsonObject.getString("name");
                System.out.println(name);
    
                JSONArray array = jsonObject.getJSONArray("arr");
                int firstValue = array.getInt(0);
                System.out.println(firstValue);
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    对Promise的研究4
    对Promise的研究3
    对Promise的研究2
    对promise的研究1
    数据结构_栈
    数据结构_队列(普通队列和双端队列)
    数据结构_链表(单链表,单向循环链表,双链表)
    数据库进行参数化,查询一行或多行语句
    数据库基础应用
    选择排序
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7048199.html
Copyright © 2020-2023  润新知