Android解析json数据

本文介绍了一个使用Java编写的Android应用程序,该程序通过HTTP请求从服务器获取JSON格式的数据,并解析这些数据以显示网站信息、入口信息等。文章展示了如何使用org.json库处理JSON对象,以及如何利用Apache HttpClient进行网络通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.org.json;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.org.utils.ServerUtils;

public class MainActivity extends Activity {

	private String Url="http://api.k780.com:88/?app=entry.qihu&domain=www.baidu.com&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
	private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv=(TextView)findViewById(R.id.TestJson);
 	    //StrictModeUtil.setStrictMode();
 	    
        String text=ServerUtils.getContent(Url);
        try {
			 JSONObject json=new JSONObject(text);
			  
			 String success=json.get("success").toString();
			 
			 JSONObject result=(JSONObject)json.get("result");
			 String website=result.get("website").toString();
			 String entry=result.get("entry").toString();
			 String update=result.get("update").toString();
	 
	      
	         tv.setText(success+entry+update+website);
	         //tv.setMovementMethod(LinkMovementMethod.getInstance());
		} catch (JSONException e) {
			e.printStackTrace();
		}
    }
    
}

 

 

package com.org.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServerUtils {
	
	/***
	 * 抓取服务端的内容
	 * @param url
	 * @return
	 */
	public static String getContent(String url){
		HttpClient client=new DefaultHttpClient();
		HttpEntity httpEntity=null;
		String result="";
		try {
			HttpGet post=new HttpGet(url);
			HttpResponse httpResponse = client.execute(post);
		    httpEntity=httpResponse.getEntity();
			if(httpEntity!=null){
				result=EntityUtils.toString(httpEntity, "UTF-8").trim();
				return result; 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				httpEntity.consumeContent();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值