Android之异步任务AsyncTask解析Json数据

一、服务器端 生成json数据

①在eclipse中创建Web->Dynamic web Project 工程名命为Web_AsyncTask_Json。

②创建CityAction.java类(Servlet类)。此时会出错,原因是servlet-api.jar没有导入工程中,步骤如下:

工程名右键->Properties->Java Build Path->Libraries->Add External JARS->找到Apache-tomcat-7.0.34文件中lib文件夹下的servlet-api.jar(D:\java2EE\apache-tomcat-7.0.34\lib\servlet-api.jar)选中->打开->ok

package com.city.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

/**
 * Servlet implementation class CityAction
 */
@WebServlet("/CityAction")
public class CityAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CityAction() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter writer=response.getWriter();
		List list=CityDataSource.getCitysList();//生成json对象,json数据一定要带
		Map>map=new HashMap>();
		map.put("citys", list);
		String jsonString=JSONSerializer.toJSON(map).toString();
		writer.println(jsonString);//注意生成json字符串时,这个地方一定要是独立干净的
		writer.flush();
		writer.close();
	}

}

doPost()方法中还可以这样写

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter writer=response.getWriter();
		String type=request.getParameter("type");
		if(type.equals("json")){
			List list=CityDataSource.getCitysList();//生成json对象,json数据一定要带
			Map>map=new HashMap>();
			map.put("citys", list);
			String jsonString=JSONSerializer.toJSON(map).toString();
			writer.println(jsonString);//注意生成json字符串时,这个地方一定要是独立干净的
		}
		
		writer.flush();
		writer.close();
	}

如果按上面的写法,对应的MainActivity.java中的CITY_PATH

private final String CITY_PATH="http://192.168.1.100:8080/Web_AsyncTask_Json/CityAction?type=json";

③提供简单的数据源:声明一个类CityDataSource.java

当用到JSONSerializer类时,要引入包json-lib-2.2.2-jdk15.jar,步骤如下:

把json-lib-2.2.2-jdk15.jar复制粘贴到工程文件下lib文件夹中刷新

package com.city.action;

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

public class CityDataSource {

	public CityDataSource() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 提供数据源
	 * @return
	 */
	public static List getCitysList(){
		List list=new ArrayList();
		list.add("北京");
		list.add("上海");
		list.add("广州");
		list.add("深圳");
		return list;
	}
}

④启动tomcat(正常启动),在地址栏输入Ip地址http://192.168.1.100:8080/Web_AsyncTask_Json/CityAction回车

可能出现错误:HTTP Status 500-Servlet exection threw an exception

解决:把commons-beanutils-1.7.jar
commons-lang.jar
ezmorph.jar

复制粘贴到lib中,此时还会报错,导入(不是复制粘贴)commons-collections.jar
commons-logging-1.1.1.jar

导入步骤:工程名右键->Properties->Java Build Path->Libraries->Add External JARS->找到工程文件中lib文件夹下的commons-collections.jar和commons-logging-1.1.1.jar选中->打开->ok

问题解决,运行显示json数据

 {"citys":["北京","上海","广州","深圳"]} 

二、Android手机客户端取出json数据

①添加网络授权,步骤如下

AndroidManifest.xml->单击Permission->单击Add->选中uses-permission ->在Name中找到INTERNET->点击下面的AndroidManifest.xml保存

②把数据展现在Spinner中,在activity_main.xml中



    

    
    


③新建一个包com.example.android_asynctask_json.http,创建http工具类HttpUtils.java

package com.example.android_asynctask_json.http;

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

public class HttpUtils {

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * @param path  获取指定路径的json信息
	 * @param encoding  指定编码格式
	 * @return
	 */
	public static String sendPostMethod(String path,String encoding){
		String result="";
		HttpClient httpClient=new DefaultHttpClient();
		try {
			HttpPost post=new HttpPost(path);
			HttpResponse response=httpClient.execute(post);
			if(response.getStatusLine().getStatusCode()==200){
				result=EntityUtils.toString(response.getEntity(), encoding);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			httpClient.getConnectionManager().shutdown();//关闭连接
		}
		return result;
		
	}
}

④新建一个包com.example.android_asynctask_json.json,创建一个json工具类JsonTools.java

package com.example.android_asynctask_json.json;

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

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

public class JsonTools {

	public JsonTools() {
		// TODO Auto-generated constructor stub
	}
	
	public static List parseList(String jsonString){
		List list=new ArrayList();
		try {
			JSONObject jsonObject=new JSONObject(jsonString);//得到 {"citys":["北京","上海","广州","深圳"]} 这个大对象
			JSONArray jsonArray=jsonObject.getJSONArray("citys");
			for(int i=0;i

MainActivity.java

package com.example.android_asynctask_json;

import java.util.List;

import com.example.android_asynctask_json.http.HttpUtils;
import com.example.android_asynctask_json.json.JsonTools;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

	private Spinner spinner1;
	private final String CITY_PATH="http://192.168.1.100:8080/Web_AsyncTask_Json/CityAction";
	private ArrayAdapter adapter;
	private ProgressDialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		spinner1=(Spinner) this.findViewById(R.id.spinner1);//加载Spinner控件
		//adapter=new ArrayAdapter(context, resource, textViewResourceId);
		//spinner1.setAdapter(adapter);//这句话执行完之后,数据就已经有了,所以这行代码应该在另外一个线程中实现,然后再更新UI
		dialog=new ProgressDialog(MainActivity.this);
		dialog.setTitle("提示");
		dialog.setMessage("正在加载...");
		new MyTask().execute(CITY_PATH);//执行异步任务
		
	}

	class MyTask extends AsyncTask>{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.show();
		}
		
		@Override
		protected void onPostExecute(List result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			adapter=new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, result);//构建适配器
			spinner1.setAdapter(adapter);
			dialog.dismiss();
		}
		
		/**
		 * 注意:在做Android开发过程中HttpUtils和JsonTools工具类是必须要有的
		 * 在一个项目中json数据格式有可能有四到五种,所以工具类里的方法要有四到五个方法来解析
		 */
		@Override
		protected List doInBackground(String... params) {
			// TODO Auto-generated method stub
			List list=null;
			String jsonString=HttpUtils.sendPostMethod(params[0], "utf-8");
			list=JsonTools.parseList(jsonString);
			return list;
		}
		//List这个结果表示json解析之后的结果,结果会很快塞到适配器当中
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

运行效果图如下

Android之异步任务AsyncTask解析Json数据_第1张图片


你可能感兴趣的:(Android)