Servlet处理Json请求数据包

来源:互联网 发布:秦义绝捏脸中文数据 编辑:程序博客网 时间:2024/05/16 09:34

http://navylee.iteye.com/blog/1558805

 
Java代码  收藏代码
  1. request.setCharacterEncoding("UTF-8");  
  2.     response.setContentType("text/html;charset=UTF-8");  
  3.     String acceptjson = "";  
  4.     try {  
  5.         BufferedReader br = new BufferedReader(new InputStreamReader(  
  6.                 (ServletInputStream) request.getInputStream(), "utf-8"));  
  7.         StringBuffer sb = new StringBuffer("");  
  8.         String temp;  
  9.         while ((temp = br.readLine()) != null) {  
  10.             sb.append(temp);  
  11.         }  
  12.         br.close();  
  13.         acceptjson = sb.toString();  
  14.         if (acceptjson != "") {  
  15.             JSONObject jo = JSONObject.fromObject(acceptjson);  
  16.             JSONArray imgArray = jo.getJSONArray("PartsImages");  
  17.             JSONArray infArray = jo.getJSONArray("BasicInfo");  
  18.             for (int i = 0; i < imgArray.size(); i++) {  
  19.                 JSONObject imgObject = JSONObject.fromObject(imgArray  
  20.                         .get(i));  
  21.                 System.out.println(imgObject.get("PartsImg"));  
  22.             }  
  23.             JSONObject infObject = JSONObject.fromObject(infArray.get(0));  
  24.             System.out.println(infObject.get("Parts_cate"));  
  25.             System.out.println(infObject.get("Company"));  
  26.             System.out.println(infObject.get("Parts_name"));  
  27.             System.out.println(infObject.get("TEL"));  
  28.             System.out.println(infObject.get("Parts_price"));  
  29.             System.out.println(infObject.get("Suitable"));  
  30.             System.out.println(infObject.get("UsedStyle"));  
  31.             System.out.println(infObject.get("Supplement"));  
  32.             System.out.println(jo.toString());  
  33.         }  
  34.         response.getWriter().write(MyReadFile.read("/post/publishsuccess"));  
  35.     } catch (Exception e) {  
  36.         e.printStackTrace();  
  37.         response.getWriter().write(MyReadFile.read("/post/publishfailure"));  
  38.     }  



Java代码  收藏代码
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.List;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.client.ClientProtocolException;  
  9. import org.apache.http.client.methods.HttpPost;  
  10. import org.apache.http.entity.StringEntity;  
  11. import org.apache.http.impl.client.DefaultHttpClient;  
  12. import org.json.JSONArray;  
  13. import org.json.JSONException;  
  14. import org.json.JSONObject;  
  15. import org.json.JSONStringer;  
  16.   
  17. import android.content.Context;  
  18. import android.telephony.NeighboringCellInfo;  
  19. import android.telephony.TelephonyManager;  
  20.   
  21. public class Location {  
  22.     public static String LOCATIONS_URL = "http://www.google.com/loc/json";  
  23.   
  24.     public static String getLocations(Context context) {  
  25.         // generate json request  
  26.         String jr = generateJsonRequest(context);  
  27.   
  28.         try {  
  29.             DefaultHttpClient client = new DefaultHttpClient();  
  30.   
  31.             StringEntity entity = new StringEntity(jr);  
  32.   
  33.             HttpPost httpost = new HttpPost(LOCATIONS_URL);  
  34.             httpost.setEntity(entity);  
  35.   
  36.             HttpResponse response = client.execute(httpost);  
  37.   
  38.             String locationsJSONString = getStringFromHttp(response.getEntity());  
  39.               
  40.             return extractLocationsFromJsonString(locationsJSONString);  
  41.         } catch (ClientProtocolException e) {  
  42.   
  43.             //e.printStackTrace();  
  44.         } catch (IOException e) {  
  45.               
  46.             //e.printStackTrace();  
  47.         } catch (Exception e) {  
  48.             //e.printStackTrace();  
  49.         }  
  50.         return null;  
  51.   
  52.     }  
  53.   
  54.     private static String extractLocationsFromJsonString(String jsonString) {  
  55.         String country = "";  
  56.         String region = "";  
  57.         String city = "";  
  58.         String street = "";  
  59.         String street_number = "";  
  60.         double latitude = 0.0;  
  61.         double longitude = 0.0;  
  62.           
  63.         //"accuracy":901.0  
  64.         double accuracy = 0.0;  
  65.           
  66.         try {  
  67.             JSONObject jo = new JSONObject(jsonString);  
  68.             JSONObject location = (JSONObject) jo.get("location");  
  69.             latitude = (Double) location.get("latitude");  
  70.             longitude = (Double) location.get("longitude");  
  71.             accuracy = (Double) location.get("accuracy");  
  72.             JSONObject address = (JSONObject) location.get("address");  
  73.   
  74.             country = (String) address.get("country");  
  75.             region = (String) address.get("region");  
  76.             city = (String) address.get("city");  
  77.             street = (String) address.get("street");  
  78.             street_number = (String) address.get("street_number");  
  79.         } catch (JSONException e) {  
  80.   
  81.             //e.printStackTrace();  
  82.         }  
  83.   
  84.         return "(" + latitude + "," + longitude + ")\t" + country + region + city  
  85.                 + street + street_number + "\t" + "精确度:" + accuracy;  
  86.     }  
  87.   
  88.     // 获取所有的网页信息以String 返回  
  89.     private static String getStringFromHttp(HttpEntity entity) {  
  90.   
  91.         StringBuffer buffer = new StringBuffer();  
  92.   
  93.         try {  
  94.             // 获取输入流  
  95.             BufferedReader reader = new BufferedReader(new InputStreamReader(  
  96.                     entity.getContent()));  
  97.   
  98.             // 将返回的数据读到buffer中  
  99.             String temp = null;  
  100.   
  101.             while ((temp = reader.readLine()) != null) {  
  102.                 buffer.append(temp);  
  103.             }  
  104.         } catch (IllegalStateException e) {  
  105.               
  106.             //e.printStackTrace();  
  107.         } catch (IOException e) {  
  108.             //e.printStackTrace();  
  109.         }  
  110.         return buffer.toString();  
  111.     }  
  112.   
  113.     private static String generateJsonRequest(Context context) {  
  114.         TelephonyManager manager = (TelephonyManager) context  
  115.                 .getSystemService(Context.TELEPHONY_SERVICE);  
  116.         List<NeighboringCellInfo> cellList = manager.getNeighboringCellInfo();  
  117.   
  118.         if (cellList.size() == 0)  
  119.             return null;  
  120.   
  121.         JSONStringer js = new JSONStringer();  
  122.         try {  
  123.   
  124.             js.object();  
  125.   
  126.             js.key("version").value("1.1.0");  
  127.             js.key("host").value("maps.google.com");  
  128.             js.key("home_mobile_country_code").value(460);  
  129.             js.key("home_mobile_network_code").value(0);  
  130.             js.key("radio_type").value("gsm");  
  131.             js.key("request_address").value(true);  
  132.             js.key("address_language").value("zh_CN");  
  133.   
  134.             JSONArray ct = new JSONArray();  
  135.   
  136.             for (NeighboringCellInfo info : cellList) {  
  137.   
  138.                 JSONObject c = new JSONObject();  
  139.                 c.put("cell_id", info.getCid());  
  140.                 c.put("location_area_code", info.getLac());  
  141.                 c.put("mobile_country_code"460);  
  142.                 c.put("mobile_network_code"0);  
  143.                 c.put("signal_strength", info.getRssi()); // 获取邻居小区信号强度  
  144.   
  145.                 ct.put(c);  
  146.             }  
  147.             js.key("cell_towers").value(ct);  
  148.             js.endObject();  
  149.         } catch (JSONException e) {  
  150.             //e.printStackTrace();  
  151.             return null;  
  152.         }  
  153.   
  154.         return js.toString().replace("true""True");  
  155.     }  
  156. }  



发送的数据为 
{"version":"1.1.0","host":"maps.google.com","home_mobile_country_code":460,"home_mobile_network_code":0,"radio_type":"gsm","request_address":True,"address_language":"zh_CN","cell_towers":[{"mobile_network_code":0,"location_area_code":24803,"cell_id":4364,"signal_strength":24,"mobile_country_code":460},{"mobile_network_code":0,"location_area_code":24803,"cell_id":4361,"signal_strength":27,"mobile_country_code":460},{"mobile_network_code":0,"location_area_code":24611,"cell_id":4381,"signal_strength":28,"mobile_country_code":460},{"mobile_network_code":0,"location_area_code":22831,"cell_id":2702,"signal_strength":29,"mobile_country_code":460},{"mobile_network_code":0,"location_area_code":24802,"cell_id":14962,"signal_strength":99,"mobile_country_code":460},{"mobile_network_code":0,"location_area_code":24802,"cell_id":4703,"signal_strength":22,"mobile_country_code":460}]}

接受到的json数据为 


"location": 

"latitude":24.476252, 
"longitude":118.16445, 
"address": 

"country":"中国", 
"country_code":"CN", 
"region":"福建省", 
"city":"厦门市", 
"street":"前埔西路", 
"street_number":"154号" 
}, 
"accuracy":901.0 
}, 
"access_token":"2:NuPu1NEsj5wuRxym:Yvvx7zIaD3oFqYiL" 


0 0
原创粉丝点击