5 Android json解析

5714 ワード

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);
    	
    }
    
}