2021-03-05 android10 json解析天气预报应用
作者:互联网
android10 json解析天气预报应用
免费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
标签:03,networkTask,String,05,android10,json,TAG,new,Log 来源: https://blog.csdn.net/yhm2046/article/details/114413494