---
title: "phonegap-connection"
author: "fanfq"
date: "2012-09-20T14:43:42Z"
tags: ["HTML5", "PhoneGap", "jQuery", "json"]
categories: ["移动开发"]
slug: "phonegap-connection"
draft: false
lastmod: "2012-09-24T22:03:14Z"
id: 1682309
url: "https://fanfq.iteye.com/blog/1682309"
---

在原有的demo基础上添加了jquery提交http请求以及返回数据的json解析。jquery不太熟好多年不用了，看样子要抓紧时间复习一下了。 index.html \[code="java"] \[/code] TestServlet.java 写了和servlet做测试用的，你可以用别的实现。 \[code="java"] protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //中文乱码问题 JSONObject jsonObj = new JSONObject(); JSONArray jsonArr = new JSONArray(); JSONObject jsonObjArr = new JSONObject(); JSONObject jsonObjArr1 = new JSONObject(); try { jsonObjArr.put("item1", "a"); jsonObjArr.put("item2", "b"); jsonArr.put(jsonObjArr); jsonObjArr1.put("item1", "c"); jsonObjArr1.put("item2", "d"); jsonArr.put(jsonObjArr1); jsonObj.put("rows", jsonArr); System.out.println(jsonObj.toString()); //{"rows":\[{"item2":"b","item1":"a"},{"item2":"d","item1":"c"}]} out.print(jsonObj.toString()); out.close(); } catch (JSONException e) { e.printStackTrace(); } } \[/code]