日期:2014-05-16  浏览次数:20365 次

Servlet处理Json请求数据包
 request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String acceptjson = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					(ServletInputStream) request.getInputStream(), "utf-8"));
			StringBuffer sb = new StringBuffer("");
			String temp;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			br.close();
			acceptjson = sb.toString();
			if (acceptjson != "") {
				JSONObject jo = JSONObject.fromObject(acceptjson);
				JSONArray imgArray = jo.getJSONArray("PartsImages");
				JSONArray infArray = jo.getJSONArray("BasicInfo");
				for (int i = 0; i < imgArray.size(); i++) {
					JSONObject imgObject = JSONObject.fromObject(imgArray
							.get(i));
					System.out.println(imgObject.get("PartsImg"));
				}
				JSONObject infObject = JSONObject.fromObject(infArray.get(0));
				System.out.println(infObject.get("Parts_cate"));
				System.out.println(infObject.get("Company"));
				System.out.println(infObject.get("Parts_name"));
				System.out.println(infObject.get("TEL"));
				System.out.println(infObject.get("Parts_price"));
				System.out.println(infObject.get("Suitable"));
				System.out.println(infObject.get("UsedStyle"));
				System.out.println(infObject.get("Supplement"));
				System.out.println(jo.toString());
			}
			response.getWriter().write(MyReadFile.read("/post/publishsuccess"));
		} catch (Exception e) {
			e.printStackTrace();
			response.getWriter().write(MyReadFile.read("/post/publishfailure"));
		}



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;

public class Location {
    public static String LOCATIONS_URL = "http://www.google.com/loc/json";

    public static String getLocations(Context context) {
        // generate json request
        String jr = generateJsonRequest(context);

        try {
            DefaultHttpClient client = new DefaultHttpClient();

            StringEntity entity = new StringEntity(jr);

            HttpPost httpost = new HttpPost(LOCATIONS_URL);
            httpost.setEntity(entity);

            HttpResponse response = client.execute(httpost);

            String locationsJSONString = getStringFromHttp(response.getEntity());
            
            return extractLocationsFromJsonString(locationsJSONString);
        } catch (ClientProtocolException e) {

            //e.printStackTrace();
        } catch (IOException e) {
            
            //e.printStackTrace();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return null;

    }

    private static String extractLocationsFromJsonString(String jsonString) {
        String country = "";
        String region = "";
        String city = "";
        String street = "";
        String street_number = "";
        double latitude = 0.0;
        double longitude = 0.0;
        
        //"accuracy":901.0
        double accuracy = 0.0;
        
        try {
            JSONObject jo = new JSONObject(jsonString);
            JSONObject location = (JSONObject) jo.get("location");
            latitude = (Double) location.get("latitude");
            longitude = (Double) location.get("longitude");
            accuracy = (Double) location.get("accuracy");
            JSONObject address = (JSONObject) location.get("address");