我从Java rest客户端调用rest api。 geoanalytics
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");
}
答案 0 :(得分:0)
使用
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("request", jsonObj.toString()));
和 评论
request.addHeader("content-type", "application/json");