Mapquest 的 JSON 响应不正确 - Android 应用
Posted
技术标签:
【中文标题】Mapquest 的 JSON 响应不正确 - Android 应用【英文标题】:Incorrect JSON response from Mapquest - Android App 【发布时间】:2016-03-21 22:20:36 【问题描述】:我正在尝试使用 mapquest api 从 mapquest 获取 JSON 对象到 android 应用程序中。 JSON请求规范如下。
POST URL:
http://www.mapquestapi.com/directions/v2/route?key=[YOUR_KEY_HERE]
POST BODY:
locations:[
"State College, PA",
"Lancaster, PA"
]
以下代码成功建立连接,但mapquest的响应不正确。
URL url_mapquest = new URL("http://www.mapquestapi.com/directions/v2/route?key=xxxxxxxxxxxx");
HttpURLConnection connection = (HttpURLConnection) url_mapquest.openConnection();
String urlParameters = "None";
connection.setRequestMethod("POST");
connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
connection.setDoOutput(true);
JSONObject jsonParam = new JSONObject();
try
JSONArray list = new JSONArray();
list.put("State College, PA");
list.put("Lancaster, PA");
jsonParam.put("locations", list);
catch (JSONException e)
e.printStackTrace();
System.out.println("JSON String: " + jsonParam.toString());
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(jsonParam.toString());
dStream.flush();
dStream.close();
int responseCode = connection.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url_mapquest);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
final StringBuilder output_Mars = new StringBuilder("Request URL : " + url_mapquest);
output_Mars.append(System.getProperty("line.separator") + "Request Parameters : " + urlParameters);
output_Mars.append(System.getProperty("line.separator") + "Response Code : " + responseCode);
output_Mars.append(System.getProperty("line.separator") + "Type : " + "POST");
String line = "";
StringBuilder responseOutput = new StringBuilder();
if (responseCode != HttpURLConnection.HTTP_FORBIDDEN)
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("output===============" + br);
while ((line = br.readLine()) != null)
responseOutput.append(line);
br.close();
else
responseOutput.append("Response Code 403 Forbidden");
以下是从 android 模拟器捕获的错误响应
代码中可能有什么问题?
参考资料:
HttpURLConnection sending JSON POST request to Apache/php JSON.simple example – Read and write JSON JSON with Java Send HTTP POST Request from Java Application to Google Messaging Service MapQuest Platform Web Services Android weather app: JSON, HTTP and Openweathermap Android HTTP Client: GET, POST, Download, Upload, Multipart Request JSON Formatter & Validator【问题讨论】:
有什么方法可以显示您发布到 api 的 JSON (jsonParam.toString())? 首先看起来你没有包含所需的路由键,但它会帮助看到像 Ultradiv 询问的 JSON。 @Ultradiv (jsonParam.toString() 是 "locations":["State College, PA","Lancaster, PA"] @RScottCarson,我故意 xxxxx 出路由键 【参考方案1】:如何将以下内容添加到连接属性中
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
模拟器响应中的以下注释表明应用程序可能未正确接收 JSON 对象。
A JSONObject text must begin with a '' at character 0
【讨论】:
以上是关于Mapquest 的 JSON 响应不正确 - Android 应用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 中解析 mapquest 地理编码 JSON