2021-03-05 android10 json解析天气预报应用

android10 json解析天气预报应用

源码:https://github.com/yhm2046/weather

免费api:http://www.weather.com.cn/data/sk/101010100.html

注意android权限

1.<uses-permission android:name="android.permission.INTERNET" /> //网络权限

2.android:usesCleartextTraffic="true"  //android高版本不许用http,加入此项简单粗暴

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.weather10">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Weather10">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

得到的jsons格式:

{"weatherinfo":
	{"city":"北京",
	"cityid":"101010100",
	"temp":"27.9",
	"WD":"南风",
	"WS":"小于3级",
	"SD":"28%",
	"AP":"1002hPa",
	"njd":"暂无实况",
	"WSE":"<3",
	"time":"17:55",
	"sm":"2.1",
	"isRadar":"1",
	"Radar":"JC_RADAR_AZ9010_JB"}
}

获取string方法:

public static String GetJsonString(String urlStr){
        URL url=null;
        StringBuffer sb =new StringBuffer();
        String line=null;
        BufferedReader buffer=null;
        try{
            url=new URL(urlStr);
            HttpURLConnection urlConn =(HttpURLConnection)url.openConnection();
            urlConn.setRequestMethod("GET");
            urlConn.setConnectTimeout(8000);
            urlConn.setReadTimeout(8000);
            buffer=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            while ((line=buffer.readLine())!=null){
                sb.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.i(TAG,"Exception-----------"+e);
        }finally {
            try {
                buffer.close();
            }catch (IOException e){
                e.printStackTrace();
                Log.i(TAG,"IOException-----------"+e);
            }
        }
        return sb.toString();
    }//end GetJsonString

测试:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(networkTask).start();
    }

    /**
     * 网络操作子线程
     */
    Runnable networkTask=new Runnable() {
        @Override
        public void run() {
            String str="http://www.weather.com.cn/data/sk/101010100.html";
            String jsonStr=WeatherUtil.GetJsonString(str);
            Log.i(TAG,"json------------->"+jsonStr);
        }
    };//end networkTask

输出结果:

I/WP_MainActivity: json------------->{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"27.9","WD":"南风","WS":"小于3级","SD":"28%","AP":"1002hPa","njd":

解析json数据方法

 public static void GetJsonData2(String jonString){
        try {
            org.json.JSONObject jsonObject=new org.json.JSONObject(jonString);
            org.json.JSONObject weatherinfo=jsonObject.getJSONObject("weatherinfo");
            Log.i(TAG,"city---------"+weatherinfo.optString("city"));
            Log.i(TAG,"temp---------"+weatherinfo.optString("temp"));
        } catch (JSONException e) {
            e.printStackTrace();
            Log.i(TAG,"JSONException---------"+e);
        }
        return;
    }//end GetJsonData2

测试:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(networkTask).start();
    }

    /**
     * 网络操作子线程
     */
    Runnable networkTask=new Runnable() {
        @Override
        public void run() {
            String str="http://www.weather.com.cn/data/sk/101010100.html";
            String jsonStr=WeatherUtil.GetJsonString(str);
            Log.i(TAG,"json------------->"+jsonStr);
            WeatherUtil.GetJsonData2(jsonStr);
        }
    };//end networkTask

输出:

I/wp_WeatherUtil_tag: city---------北京
    temp---------27.9

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值