java 请求 servlet

来源:互联网 发布:unix与windows 编辑:程序博客网 时间:2024/06/05 23:50
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"));<p>}</p><p>客户端发请求</p><p><pre class="java" name="code" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 14px; line-height: 24px;">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;    }
或者:

 HttpClient httpClient = new DefaultHttpClient();
        HttpPost postMethod = new HttpPost(url);
        
        final StringEntity entity = new StringEntity(json, "UTF-8");
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, Consts.UTF_8.toString()));
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
        
        postMethod.setEntity(entity);
      
        //
        startTime = System.currentTimeMillis();
        final HttpResponse response = httpClient.execute(postMethod);
        
        // 读取请求结果
        if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
            final HttpEntity httpEntity = response.getEntity();
            jsonResponse = EntityUtils.toString(httpEntity);
            
        }
        endTime = System.currentTimeMillis();
        System.out.println("返回:" + jsonResponse);



                                             
0 0