我正在尝试升级using this
这是我的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/elevation/json");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("locations", "40.714728,-73.998672"));
nameValuePairs.add(new BasicNameValuePair("sensor", "true"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
System.out.println("HTTP RESPONSE " + response.toString());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("RESPONSE " + responseString);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
System.out.println("ClientProtocolException " + e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOException " + e.toString());
}
我收到以下错误
无效请求缺少路径或位置参数
答案 0 :(得分:4)
HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/elevation/json?locations=39.0000,100.0000&sensor=false");
并从代码中删除名称值对。
答案 1 :(得分:2)
您正在使用数据执行HTTP POST请求。 API似乎指定了HTTP GET请求(包含URL中的数据)。
GET请求更容易制定 - 您可以使用字符串连接来构建表单中所需的URL:
http://maps.googleapis.com/maps/api/elevation/json?locations=<lat,lon>&sensor=<bool>
答案 2 :(得分:1)
您可以使用简单的HTTP GET请求获得响应。
http://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&sensor=true
答案 3 :(得分:1)
String url = "http://maps.googleapis.com/maps/api/elevation/json"
String[] parameter = {"locations", "40.714728,-73.998672", "sensor", "true"};
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
// Prepare a request object
HttpGet httpget = new HttpGet(url+getUrlParameter(parameter));
// Execute the request
HttpResponse response;
response = httpclient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
public String getUrlParameter(String[] parameter){
for(int i=0 ; i<parameter.length ; i=i+2){
if(i == 0)
urlParameter = "?" + parameter[i] + "=" + parameter[i+1];
else
urlParameter = urlParameter + "&" + parameter[i] + "=" + parameter[i+1];
}
return urlParameter;
}
此代码来自我自己的jsonlibrary,它应该适合您。从这里 HttpEntity实体将充满数据。您必须将它们分开才能在代码中使用。另请注意,此http工作必须在try-catch博客上才能捕获恶劣条件下的异常。