android PM2.5监控demo开发

时间:2023-03-08 19:03:11

最近看到了这个网站是aqicn.org,是一个监控北京空气状态的网站,截图如下android PM2.5监控demo开发

好了,接下来我们利用这个网站返回的json数据来写一个监控北京空气状况尤其是PM2.5的demo。

1.布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <TextView
        android:id="@+id/tvLoad"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="loading。。。"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnReload"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

主要代码如下:

public class MainActivity extends ActionBarActivity {
    private TextView tvLoad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mian);
        tvLoad = (TextView) findViewById(R.id.tvLoad);
        findViewById(R.id.btnReload).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                reLoad();
            }
        });
        reLoad();
    }
    private void reLoad(){
        tvLoad.setText("load...");
        new AsyncTask<Void, Void, String>() {
            StringBuffer sb= null;
            @Override
            protected String doInBackground(Void... params) {
                try {
                    URL url = new URL("http://aqicn.org/publishingdata/json");
                    BufferedReader bf = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));
                    sb = new StringBuffer();
                    String line = null;
                    while ((line = bf.readLine()) != null) {
                        sb.append(line);
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return sb.toString();
            }

            @Override
            protected void onPostExecute(String result) {
                if (result != null) {
                    try {
                        JSONArray jsonArr = new JSONArray(result);
                        JSONObject firstJson = jsonArr.getJSONObject(0);
                        JSONArray pollutants = firstJson.getJSONArray("pollutants");
                        JSONObject firstPollu = pollutants.getJSONObject(0);
                        System.out.println(String.format("%s %s:%f", firstJson.getString("cityName"),firstJson.getString("localName"),firstPollu.getDouble("value")));
                        tvLoad.setText(String.format("%s %s:%f", firstJson.getString("cityName"),firstJson.getString("localName"),firstPollu.getDouble("value")));
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                }

        }.execute();
    }
}

这个demo比较简单,至于界面还可以设计的更好看,本文只是为了技术实现。