从服务器获取Json数据,并解析

<pre name="code" class="html">xml格式:
</pre><pre name="code" class="html">{
	"fangone": 
	[
		"shuoming": "大飘窗搭配绿色乡村风格,春意盎然",
		"quyu":"长宁区",
		"louceng": "5/13层",
		"mianji": "14.0m*m",
		"chaoxiang": "朝向",
		"ditie": "地铁沿线",
		"WC": "卫生间",
		"dianshi": "有电视和阳台",
		"baojia": "2310元/月",
	]
}
</pre><pre name="code" class="html">
<pre name="code" class="html">/**
 * 获取json数据,并解析
 * @author wu
 *
 */
public class MainActivity extends Activity {
	
	List<Information> Infolist;
	String strUrl="这里是链接";		//网页链接
	String getResult;				//从网页返回的Json数据
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Infolist=new ArrayList<Information>();
        
        getResult = connServerForResult(strUrl);
        Infolist=parseJson(getResult);
        
        for (int i = 0; i < Infolist.size(); i++) {
			Log.i("Prt", Infolist.get(i).getShuoming()+Infolist.get(i).getQuyu());	//等等……不一一输出了
		}
    }
    
    /** 
     * 通过URL从服务器上下载下来,保存为字符串
     * @param strUrl  
     * @return 
     */  
    public static String connServerForResult(String strUrl) {  
        // HttpGet对象  
        HttpGet httpRequest = new HttpGet(strUrl);  
        String strResult = "";  
        try {  
            // HttpClient对象  
            HttpClient httpClient = new DefaultHttpClient();  
            // 获得HttpResponse对象  
            HttpResponse httpResponse = httpClient.execute(httpRequest);  
            // 当返回码为200时,做处理  得到服务器端返回json数据,并做处理   
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                // 取得返回的数据  
                strResult = EntityUtils.toString(httpResponse.getEntity());  
            }  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return strResult;  
    }
    
    /** 
     * 普通Json数据解析
     * @param strResult 从URL中得到的字符串 
     */  
    public static List<Information> parseJson(String strResult) {  
    	
    	Information mInformation = new Information();
    	List<Information> mList = new ArrayList<Information>();
    	
        try {  
            JSONObject mjsonObj = new JSONObject(strResult);
            JSONArray mJsonArray=mjsonObj.getJSONArray("fangone");
            mInformation.setShuoming(mJsonArray.getString(0));
            mInformation.setQuyu(mJsonArray.getString(1));
            mInformation.setLouceng(mJsonArray.getString(2));
            mInformation.setMianji(mJsonArray.getString(3));
            mInformation.setChaoxiang(mJsonArray.getString(4));
            mInformation.setDitie(mJsonArray.getString(5));
            mInformation.setWC(mJsonArray.getString(6));
            mInformation.setDianshi(mJsonArray.getString(7));
            mInformation.setBaojia(mJsonArray.getString(8));
            mList.add(mInformation);
        } catch (JSONException e) {  
            System.out.println("Json parse error");  
            e.printStackTrace();  
        }
		return mList;  
    } 
}


 


这个知识点老是记不住,每次使用都是从网上搜,特在此记下……!~



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值