java 请求 servlet

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值