繁体   English   中英

如何使用Java Rest客户端在POST负载中传递参数“ request”?

[英]How to pass parameter 'request' in POST payload using Java Rest client?

我从Java rest客户端调用rest api。 地理分析

在此处输入图片说明

 private static void BatchGeoCodingProcess() throws JSONException, ClientProtocolException, IOException { System.out.println("BatchGeoCodingProcess..................................."); /* { "coder": { "addresses": [ { "id": "2824", "countrycode": "FR", "longitude": "2.33", "latitude": "48.88", "maxdist": "20000" }, { "id": "6404", "countrycode": "FR", "street": "1598 ROUTE DE QUARANTE SOUS", "zipcode": "78630", "city": "ORGEVAL" } ] }, "encoding": "utf-8" } */ JSONObject req = new JSONObject(); JSONObject jsonObj = new JSONObject(); req.put("request", jsonObj); JSONObject coder = new JSONObject(); jsonObj.put("coder", coder); jsonObj.put("encoding", "utf-8"); JSONArray addrList = new JSONArray(); coder.put("addresses", addrList); // Reverse geo code JSONObject addr = new JSONObject(); addr.put("id", "2824"); addr.put("countrycode", "FR"); addr.put("longitude", "2.33"); addr.put("latitude", "48.88"); addrList.put(addr); // geo code addr = new JSONObject(); addr.put("id", "6404"); addr.put("countrycode", "FR"); addr.put("street", "1598 ROUTE DE QUARANTE SOUS"); addr.put("zipcode", "78630"); addr.put("city", "ORGEVAL"); addrList.put(addr); HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost("http://geowebservices.maporama.com/demo/batchcoder.json?maporamakey=2u7kdn4DnYE=&"); request.addHeader("content-type", "application/json"); //request.addHeader("Accept", "application/json"); System.out.println("test = "+req.toString()); StringEntity body = new StringEntity(req.toString()); request.setEntity(body); HttpResponse response = client.execute(request); int code = response.getStatusLine().getStatusCode(); String reason = response.getStatusLine().getReasonPhrase(); String test = EntityUtils.toString(response.getEntity()); System.out.println("Code = "+code+" :: Reason = "+reason); System.out.println("Batch geocode"); System.out.println("test..."+test); System.out.println("Batch geocode"); } 

用过的

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("request", jsonObj.toString()));

和评论

request.addHeader("content-type", "application/json");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM