JSON

手机软件技术应用教程(3)

字号+ 作者:H5之家 来源:H5之家 2016-12-13 16:00 我要评论( )

person: {"id":1,"name":"xiaoluo","address":"广州"}Person [id=1, name=xiaoluo, address=广州]------------------------------------------------persons: [{"id":1,"name":"xiaoluo","address":"广州"},{"id":2

person: {"id":1,"name":"xiaoluo","address":"广州"} Person [id=1, name=xiaoluo, address=广州] ------------------------------------------------ persons: [{"id":1,"name":"xiaoluo","address":"广州"},{"id":2,"name":"android","address":"上海"}] [Person [id=1, name=xiaoluo, address=广州], Person [id=2, name=android, address=上海]] ------------------------------------------------ String---->["广州","上海","北京"] list2---->[广州, 上海, 北京] ------------------------------------------------ Map---->[{"id":"001","age":"20","name":"xiaoluo"},{"id":"002","age":"33","name":"android"}] listMap2---->[{id=001, age=20, name=xiaoluo}, {id=002, age=33, name=android}] ------------------------------------------------

三、在Android客户端解析服务器端的json数据

下面我们来完成一个综合的例子,Android客户端通过一个AsyncTask异步任务请求服务器端的某些数据,然后在解析完这些数据后,将得到的数据内容更新到我们的Spinner这个UI控件当中。

我们首先来看下服务器端的代码:

@WebServlet("/CityServlet") public class CityServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CityServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); String type = request.getParameter("type"); if("json".equals(type)) { List<String> cities = new ArrayList<String>(); cities.add("广州"); cities.add("上海"); cities.add("北京"); cities.add("湖南"); Map<String, List<String>> map = new HashMap<String, List<String>>(); map.put("cities", cities); String citiesString = JSON.toJSONString(map); writer.println(citiesString); } writer.flush(); writer.close(); } }

如果客户端请求的参数是type=json,则响应给客户端一个json数据格式

接着来看看客户端的代码,首先看看客户端的布局文件,其实就是一个按钮和一个Spinner控件,当点击按钮后,通过http协议请求服务器端的数据,然后在接收到后再更新我们的Spinner控件的数据

<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" > <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="64dp" android:layout_marginTop="64dp" android:textSize="20sp" android:text="城市" /> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/textView1" android:layout_toRightOf="@id/textView1"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/spinner" android:layout_marginLeft="22dp" android:layout_marginTop="130dp" android:text="加载数据" /> </RelativeLayout>

在Android客户端写一个解析json数据格式的类:

public class JsonUtils { /** * @param citiesString 从服务器端得到的JSON字符串数据 * @return 解析JSON字符串数据,放入List当中 */ public static List<String> parseCities(String citiesString) { List<String> cities = new ArrayList<String>(); try { JSONObject jsonObject = new JSONObject(citiesString); JSONArray jsonArray = jsonObject.getJSONArray("cities"); for(int i = 0; i < jsonArray.length(); i++) { cities.add(jsonArray.getString(i)); } } catch (Exception e) { e.printStackTrace(); } return cities; } }

当然我们的HttpUtils类也不可少:

public class HttpUtils { /** * @param path 请求的服务器URL地址 * @param encode 编码格式 * @return 将服务器端返回的数据转换成String */ public static String sendPostMessage(String path, String encode) { String result = ""; HttpClient httpClient = new DefaultHttpClient(); try { HttpPost httpPost = new HttpPost(path); HttpResponse httpResponse = httpClient.execute(httpPost); if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); if(httpEntity != null) { result = EntityUtils.toString(httpEntity, encode); } } } catch (Exception e) { e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return result; } }

最后来看看我们的MainActivity类:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评