5 Android(安卓)json 解析
16lz
2021-01-26
package com.east.zy;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import org.apache.http.HttpConnection;import org.json.JSONException;import org.json.JSONObject;import android.R.string;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class TestActivity extends Activity { /** Called when the activity is first created. */private TextView text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(TextView)findViewById(R.id.textView); String str=this.connectWeb(); System.out.println(str); try { parseJSON(this.connectWeb());} catch (JSONException e) {// TODO: handle exceptione.printStackTrace();} } public String connectWeb() { String str=""; try { URL url=new URL("http://m.weather.com.cn/data/101180701.html");HttpURLConnection connect=(HttpURLConnection)url.openConnection();//connect.setRequestMethod("GET");//connect.setDoInput(true);InputStreamReader read=new InputStreamReader(connect.getInputStream());BufferedReader bf=new BufferedReader(read);String temp;while((temp=bf.readLine())!= null){str += temp;}//System.out.println(str);text.setText(str);} catch (Exception e) {e.printStackTrace();} return str; /* { "weatherinfo": { "city": "南阳", "city_en": "nanyang", "date_y": "2013年12月31日", "date": "", "week": "星期二", "fchh": "11", "cityid": "101180701", "temp1": "15℃~0℃", "temp2": "14℃~0℃", "temp3": "12℃~0℃", "temp4": "14℃~-1℃", "temp5": "13℃~0℃", "temp6": "11℃~-1℃", "tempF1": "59℉~32℉", "tempF2": "57.2℉~32℉", "tempF3": "53.6℉~32℉", "tempF4": "57.2℉~30.2℉", "tempF5": "55.4℉~32℉", "tempF6": "51.8℉~30.2℉", "weather1": "晴", "weather2": "晴", "weather3": "多云", "weather4": "晴", "weather5": "晴", "weather6": "阴转晴", "img1": "0", "img2": "99", "img3": "0", "img4": "99", "img5": "1", "img6": "99", "img7": "0", "img8": "99", "img9": "0", "img10": "99", "img11": "2", "img12": "0", "img_single": "0", "img_title1": "晴", "img_title2": "晴", "img_title3": "晴", "img_title4": "晴", "img_title5": "多云", "img_title6": "多云", "img_title7": "晴", "img_title8": "晴", "img_title9": "晴", "img_title10": "晴", "img_title11": "阴", "img_title12": "晴", "img_title_single": "晴", "wind1": "微风", "wind2": "微风", "wind3": "微风", "wind4": "微风", "wind5": "微风", "wind6": "微风", "fx1": "微风", "fx2": "微风", "fl1": "小于3级", "fl2": "小于3级", "fl3": "小于3级", "fl4": "小于3级", "fl5": "小于3级", "fl6": "小于3级", "index": "冷", "index_d": "天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。", "index48": "冷", "index48_d": "天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。", "index_uv": "中等", "index48_uv": "中等", "index_xc": "适宜", "index_tr": "适宜", "index_co": "舒适", "st1": "15", "st2": "2", "st3": "14", "st4": "2", "st5": "13", "st6": "0", "index_cl": "适宜", "index_ls": "基本适宜", "index_ag": "极不易发" }} * */ } /** * 无数组 JSON 解析 * @param str * @throws JSONException */ public void parseJSON(String str) throws JSONException {// StringBuilder builder=new StringBuilder();// builder.append(str); JSONObject object=new JSONObject(str); JSONObject obj=object.getJSONObject("weatherinfo"); String str1=obj.getString("city"); String str2=obj.getString("index_ls"); System.out.println(str2); text.setText(str1); } }
更多相关文章
- android assets文件夹下的数据库应用
- Android(安卓)改变当前屏幕背光亮度
- Android限制EditText输入金额小于1E,并最多2位小数
- 解决了在Android原生TextView中,当text长度小于hint时,TextView右
- Android(安卓)json通信(解析)方法
- Android(安卓)Studio 构建时报错:与元素类型 “item” 相关联的
- Android:国家气象局天气预报接口JSON完全解析
- Android中如何判断sd卡容量是否小于某值
- Android——Json和Gson分别是什么,以及Json 数据的解析方法