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<String> list=CityDataSource.getCitysList();//生成json物件,json資料一定要帶<K,V> Map<String, List<String>>map=new HashMap<String, List<String>>(); 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<String> list=CityDataSource.getCitysList();//生成json物件,json資料一定要帶<K,V> Map<String, List<String>>map=new HashMap<String, List<String>>(); 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<String> getCitysList(){
List<String> list=new ArrayList<String>();
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中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_asynctask_json.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="102dp"
android:textSize="20sp"
android:text="所屬城市:" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/textView1" />
</RelativeLayout>
③新建一個包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<String> parseList(String jsonString){
List<String> list=new ArrayList<String>();
try {
JSONObject jsonObject=new JSONObject(jsonString);//得到 {"citys":["北京","上海","廣州","深圳"]} 這個大物件
JSONArray jsonArray=jsonObject.getJSONArray("citys");
for(int i=0;i<jsonArray.length();i++){
list.add(jsonArray.getString(i));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
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<String> 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<String>(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<String, Void, List<String>>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
@Override
protected void onPostExecute(List<String> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, result);//構建介面卡
spinner1.setAdapter(adapter);
dialog.dismiss();
}
/**
* 注意:在做Android開發過程中HttpUtils和JsonTools工具類是必須要有的
* 在一個專案中json資料格式有可能有四到五種,所以工具類裡的方法要有四到五個方法來解析
*/
@Override
protected List<String> doInBackground(String... params) {
// TODO Auto-generated method stub
List<String> list=null;
String jsonString=HttpUtils.sendPostMethod(params[0], "utf-8");
list=JsonTools.parseList(jsonString);
return list;
}
//List<String>這個結果表示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);
}
}
執行效果圖如下