资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

安卓异步处理json解析例子

首先有个工具类: Myhttp.class

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、高平网络推广、重庆小程序开发、高平网络营销、高平企业策划、高平品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供高平建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

package com.example.json;

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

然后主类

package com.example.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	ListView list;
	ArrayList> myArrayList = new ArrayList>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		list = (ListView) findViewById(R.id.list);

		new getJsonTask().execute(null, null, null);

	}

	class getJsonTask extends AsyncTask {

		@Override
		protected Object doInBackground(Object... params) {
			String url = "http://www.baidu.com";
			Myhttp myHttp = new Myhttp();
			String retStr = myHttp.httpGet(url);

			try {
				JSONObject json1 = new JSONObject(retStr);
				String j1 = json1.getString("kinds");
				JSONArray j2 = new JSONArray(j1);
				for (int i = 0; i < j2.length(); i++) {
					JSONObject jsonObject = j2.getJSONObject(i);
					String j3 = jsonObject.getString("breeds");
					JSONArray j4 = new JSONArray(j3);
					for (int j = 0; j < j4.length(); j++) {
						JSONObject jsonObject1 = j4.getJSONObject(j);
						String j6 = jsonObject1.getString("id");
						String j7 = jsonObject1.getString("breedName");
						HashMap map = new HashMap();
						map.put("id", j6);
						map.put("name", j7);
						myArrayList.add(map);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			start();
		}
	}

	private void start() {

		SimpleAdapter mySimpleAdapter = new SimpleAdapter(this, myArrayList,
				R.layout.list_item, new String[] { "id", "name" }, new int[] {
						R.id.name, R.id.age });

		list.setAdapter(mySimpleAdapter);

		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				
				@SuppressWarnings("unchecked")
				HashMap map = (HashMap) list
						.getItemAtPosition(position);
				String title = map.get("id");
				String content = map.get("name");
				Toast.makeText(
						getApplicationContext(),
						"你选择了第" + position + "个Item,itemTitle的值是:" + title
								+ "itemContent的值是:" + content,
						Toast.LENGTH_SHORT).show();
			}
		});
	}
}

新闻名称:安卓异步处理json解析例子
文章URL:http://cdkjz.cn/article/ijgcpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220