5 Android json 解析
来源:互联网 发布:知乎 程序员 收入 编辑:程序博客网 时间:2024/06/10 03:52
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); } }
0 0
- 5 Android json 解析
- 【json】android json解析
- Android-Android解析JSON
- Android数据解析:Json解析
- android---Json数据解析
- Android Json 数据解析
- android 解析JSON
- android 解析json数据格式
- android 解析json数据格式
- Android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- Android的JSON解析
- Android 解析Json
- android JSON 解析
- Android 解析json数据格式
- android解析Json
- Android解析json数据
- vmware linux下配置 bridged网络
- A practical guide to SSE SIMD with C++
- 闭包学习
- LeetCode | Longest Valid Parentheses
- epoll lt与et模式区别
- 5 Android json 解析
- 利用OpenCV实现图像边缘直方图输出
- 如何过滤 adb logcat 输出
- IOS 传值方法总结
- jvm性能调优
- constraint auto layout
- poj1604Just the Facts(数论)
- 关于windhunter -> newwindhunter -> windhunterSB 的年度总结
- ClassPathXmlApplicationContext加载多个XML文件