android 异步线程刷新UI 以及 JSON解析 以及 url get请求

import android.os.Handler;
import android.os.Message;

 1.

Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    progressBar.setVisibility(View.GONE);
                    myAdapter.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
        }

    };

 

 Message message = new Message();
            message.what = 1;
            mHandler.sendMessage(message);

 

 

2.json解析:解析数据:

{
  "showapi_res_code": 0,
  "showapi_res_error": "",
  "showapi_res_body": {
    "ret_code": 0,
    "list": [
      {
        "prov": "河北",
        "p90": "5.77",
        "p0": "5.83",
        "p95": "6.69",
        "p97": "6.58",
        "p89": "5.48",
        "p92": "6.34",
        "ct": "2017-05-31 07:02:32.408",
        "p93": "6.22"
      },
      {
        "prov": "山西",
        "p90": "6.28",
        "p0": "6.08",
        "p95": "6.81",
        "p97": "6.89",
        "p89": "5.89",
        "p92": "6.31",
        "ct": "2017-05-31 07:02:32.403",
        "p93": "6.39"
      },
      {
        "prov": "内蒙古",
        "p90": "6.46",
        "p0": "6.69",
        "p95": "6.77",
        "p97": "7.25",
        "p89": "",
        "p92": "6.31",
        "ct": "2017-05-31 07:02:32.396",
        "p93": "6.75"
      },
      {
        "prov": "辽宁",
        "p90": "5.67",
        "p0": "5.75",
        "p95": "6.74",
        "p97": "6.63",
        "p89": "5.86",
        "p92": "6.32",
        "ct": "2017-05-31 07:02:32.394",
        "p93": "6.21"
      },
      {
        "prov": "吉林",
        "p90": "5.62",
        "p0": "5.76",
        "p95": "6.81",
        "p97": "6.69",
        "p89": "6.1",
        "p92": "6.31",
        "ct": "2017-05-31 07:02:32.389",
        "p93": "6.2"
      },
      {
        "prov": "黑龙江",
        "p90": "5.59",
        "p0": "5.03",
        "p95": "6.51",
        "p97": "6.08",
        "p89": "",
        "p92": "6.14",
        "ct": "2017-05-31 07:02:32.412",
        "p93": "5.66"
      },
      {
        "prov": "上海",
        "p90": "5.79(沪89号)",
        "p0": "5.81",
        "p95": "6.72",
        "p97": "6.6(沪95号)",
        "p89": "6.02",
        "p92": "6.31",
        "ct": "2017-05-31 07:02:32.389",
        "p93": "6.2(沪92号)"
      },
      
    ]
  }
}

 解析语句:

 

 

 //解析多个数据的Json
    private void parseJsonMulti(String strResult) {

        try {
            JSONArray jsonObjs = new JSONObject(strResult).getJSONObject("showapi_res_body").getJSONArray("list");
            String s = "";
            for(int i = 0; i < jsonObjs.length() ; i++){

                JSONObject jsonObj = (JSONObject)jsonObjs.get(i);
                Petrol petrol = new Petrol();
                petrol.setProv(jsonObj.getString("prov"));
                petrol.setP0(jsonObj.getString("p0"));
                petrol.setP89(jsonObj.getString("p89"));
                petrol.setP90(jsonObj.getString("p90"));

                petrol.setP92(jsonObj.getString("p92"));
                petrol.setP93(jsonObj.getString("p93"));
                petrol.setP95(jsonObj.getString("p95"));
                petrol.setP97(jsonObj.getString("p97"));

            }

        } catch (JSONException e) {
            System.out.println("Jsons parse error !");
            e.printStackTrace();
        }
    }

 json解析的时候可以使用opting,和getstring的区别就是

optString会在得不到你想要的值时候返回空字符串”“,而getString会抛出异常。

可以解决服务器字段缺少或者没有该字段而导致的异常以至于程序崩溃。

url get请求

URLConnection connection = null;
                BufferedReader br = null;
                try {

                    StringBuffer sb = new StringBuffer();
                    URL url = new URL("http://route.showapi.com/138-46?showapi_appid=38170&showapi_sign=4860f68a39034b48a9214e5f38eded75&prov=");
                    connection = url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    connection.connect();


                    InputStream is = connection.getInputStream();
                    InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                    br = new BufferedReader(isr);
                    String strRead = null;
                    while ((strRead = br.readLine()) != null) {
                        sb.append(strRead);



                    }

                   parseJsonMulti(sb.toString());

catch (Exception e) {
e.printStackTrace();
System.out.println("发送get请求出现异常!" + e);
}finally {

}

 

posted @ 2017-05-31 09:11  新年新气象  阅读(283)  评论(0编辑  收藏  举报