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 exception
e.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);
}
}
5 Android json 解析
最新推荐文章于 2025-04-24 17:05:49 发布