2016-06-07 1 views
0

Ich rufe Rest API von Java Rest Client. geoanalyticsWie übergeben Parameter 'Anfrage' in POST-Payload mit Java Rest Client?

enter image description here

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

+0

Was ist der Fehler, den Sie sehen? Kann nicht viel ohne es helfen ... –

Antwort

0

Gebrauchte

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

und kommentiert

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