Android 解析JSON数据填充到Listview中

JSON代码格式化:http://blog.92coding.com/tool/jsonformat.html

用GSON处理JSON数据:http://www.cnblogs.com/VinC/archive/2011/02/25/Use-GSon-Hand-JsonData-For-Android-Device.html

新浪微博开发Json解析:http://www.cnblogs.com/xiaoxiongbuwawa/archive/2012/10/11/2718551.html

Json数据解析:http://www.open-open.com/bbs/view/1319448346593

Json解析及简单例子:http://www.open-open.com/lib//view/open1326376799874.html

类似的json数组数据

[java] view plain copy
print ?
  1. [  
  2.      {  
  3.         "screen_name""阿信",  
  4.          "followers_count"11091994,  
  5.          "uid"1265020392  
  6.      },  
  7.      {  
  8.          "screen_name""阿信星闻",  
  9.          "followers_count"22427,  
  10.          "uid"1822541757  
  11.      }  
  12.   
  13.  ]  
[
     {
        "screen_name": "阿信",
         "followers_count": 11091994,
         "uid": 1265020392
     },
     {
         "screen_name": "阿信星闻",
         "followers_count": 22427,
         "uid": 1822541757
     }

 ]

为了简洁方便首先写一个向服务器发送请求得到相应数据的类:

[java] view plain copy
print ?
  1. package com.wmh.googledoodles;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.HttpStatus;  
  7. import org.apache.http.client.ClientProtocolException;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.util.EntityUtils;  
  12.   
  13.   
  14.   
  15. public class MyHttp  
  16. {  
  17.     public String httpGet(String url)  
  18.     {  
  19.         String response = null;  
  20.         HttpClient httpClient = new DefaultHttpClient();  
  21.         //创建HttpGet对象  
  22.         HttpGet httpGet = new HttpGet(url);  
  23.         HttpResponse httpResponse;  
  24.         try  
  25.         {  
  26.             //使用execute方法发送 HttpGet请求,并返回httRresponse对象  
  27.             httpResponse = httpClient.execute(httpGet);  
  28.             int statusCode = httpResponse.getStatusLine().getStatusCode();  
  29.             if(statusCode==HttpStatus.SC_OK)  
  30.             {  
  31.                 //获得返回结果  
  32.                 response=EntityUtils.toString(httpResponse.getEntity());  
  33.             }  
  34.               
  35.         } catch (ClientProtocolException e)  
  36.         {  
  37.               
  38.             e.printStackTrace();  
  39.         } catch (IOException e)  
  40.         {  
  41.               
  42.             e.printStackTrace();  
  43.         }  
  44.           
  45.         return response;  
  46.     }  
  47. }  
package com.wmh.googledoodles;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
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 MyHttp
{
	public String httpGet(String url)
	{
		String response = null;
		HttpClient httpClient = new DefaultHttpClient();
		//创建HttpGet对象
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse;
		try
		{
			//使用execute方法发送 HttpGet请求,并返回httRresponse对象
			httpResponse = httpClient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if(statusCode==HttpStatus.SC_OK)
			{
				//获得返回结果
				response=EntityUtils.toString(httpResponse.getEntity());
			}
			
		} catch (ClientProtocolException e)
		{
			
			e.printStackTrace();
		} catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return response;
	}
}

再在MainActivity中获取JSON数据,解析出数据内容,填充进适配器,再绑定到Listview控件上

[java] view plain copy
print ?
  1. public class MainActivity extends Activity  
  2. {  
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState)  
  6.     {   
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         List> ListData = getListData();  
  11.         SimpleAdapter ListAdapter = new SimpleAdapter(this,ListData, R.layout.list_item,     
  12.                                                                      new String []{"screen_name","uid"},   
  13.                                                                      new int []{R.id.name,R.id.uid});  
  14.           
  15.         ListView list = (ListView)findViewById(R.id.list);  
  16.         list.setAdapter(ListAdapter);  
  17.           
  18.     }  
  19.       
  20.     private List> getListData()  
  21.     {  
  22.         List> Data = new ArrayList>();  
  23.         String url = "XXXXXX";  
  24.         MyHttp myHttp = new MyHttp();  
  25.         String retStr = myHttp.httpGet(url);  
  26.           
  27.         try  
  28.         {  
  29.             JSONArray jsonArray = new JSONArray(retStr);  
  30.                 for (int i = 0; i < jsonArray.length(); i++)  
  31.                   {  
  32.                       JSONObject jsonObject = jsonArray.getJSONObject(i);;  
  33.                       {  
  34.                         HashMap hashMap = new HashMap();  
  35.                         hashMap.put("screen_name", jsonObject.getString("screen_name"));  
  36.                         hashMap.put("uid", jsonObject.getString("uid"));  
  37.                                                 Data.add(hashMap);                     
  38.                       }  
  39.                   }  
  40.           
  41.         } catch (JSONException e)  
  42.         {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.           
  47.         return Data;  
  48.           
  49.     }       
  50.   
  51. }  
public class MainActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{ 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		List> ListData = getListData();
		SimpleAdapter ListAdapter = new SimpleAdapter(this,ListData, R.layout.list_item,   
                                                                     new String []{"screen_name","uid"}, 
                                                                     new int []{R.id.name,R.id.uid});
		
		ListView list = (ListView)findViewById(R.id.list);
		list.setAdapter(ListAdapter);
		
	}
	
	private List> getListData()
	{
		List> Data = new ArrayList>();
		String url = "XXXXXX";
		MyHttp myHttp = new MyHttp();
		String retStr = myHttp.httpGet(url);
		
		try
		{
			JSONArray jsonArray = new JSONArray(retStr);
				for (int i = 0; i < jsonArray.length(); i++)
			      {
			          JSONObject jsonObject = jsonArray.getJSONObject(i);;
			          {
						HashMap hashMap = new HashMap();
						hashMap.put("screen_name", jsonObject.getString("screen_name"));
						hashMap.put("uid", jsonObject.getString("uid"));
                                                Data.add(hashMap);			          
			          }
			      }
		
		} catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return Data;
		
	}     

}


   

你可能感兴趣的:(Android 解析JSON数据填充到Listview中)