我在调一个高德地图的接口,其中url需要传入省份的简称,但是总报格式错误,我也尝试的去设置编码统一,还是报错。代码如下,错误在最后,恳请大佬们指点!package cn.bulusi;import java.beans.Encoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;public class MapTest {
static String strKey = "a6de56c08c9c6edf828e6469ac83dfc4";
public static void main(String[] args) {
String start = "上海市安亭地铁站";
String end = "上海市南翔地铁站";
//String waypoints = "东方肝胆外科医院";
String startLonLat = getLonLat(start);//调用返回经纬度信息的方法
String endLonLat = getLonLat(end);//调用返回经纬度信息的方法
//String wp = getLonLat(waypoints); System.out.println(startLonLat);//打印经纬度
System.out.println(endLonLat);//打印经纬度
Long dis = getDistance(startLonLat, endLonLat);//传入经纬度,返回距离的方法
System.out.println(dis);//打印距离
} @SuppressWarnings("static-access")
private static String getLonLat(String address) {
//返回输入地址address的经纬度信息, 格式是 经度,纬度
String queryUrl = "https://restapi.amap.com/v3/geocode/geo?key="+strKey+"&address=" + address;
@SuppressWarnings("unused")
Encoder en = new Encoder();
String queryResult = getResponse(queryUrl); //高德返回的是JSON格式的字符串
JSONObject jo = new JSONObject();
JSONObject j = jo.fromObject(queryResult);//将JSON格式的字符串转换为java对象
//JSONObject jo = new JSONObject().fromObject(queryResult);
JSONArray ja = j.getJSONArray("geocodes");
return new JSONObject().fromObject(ja.getString(0)).get("location").toString();
} @SuppressWarnings({ "static-access", "unused" })
private static Long getDistance(String startLonLat, String endLonLat) {
//返回起始地startAddr与目的地endAddr之间的距离,单位:米
Long result = new Long(0);
String queryUrl = "https://restapi.amap.com/v4/direction/truck?key="+strKey+"&origin="+startLonLat+"&destination="
+endLonLat+"&size=1"+"&diu=imei"+"&strategy=1"+"&height=1.6"+"&width=2.5"+"&load=0.9"+"&weight=10"+"&axis=2&province=沪";
String queryResult = getResponse(queryUrl);//高德返回的是JSON格式的字符串
JSONObject jo = new JSONObject().fromObject(queryResult);
jo = (JSONObject) jo.get("data");
jo = (JSONObject) jo.get("route");
JSONArray ja = jo.getJSONArray("paths");
result = Long.parseLong(new JSONObject().fromObject(ja.getString(0)).get("distance").toString());
return result;
// return queryResult;
} private static String getResponse(String serverUrl) {
//用JAVA发起http请求,并返回json格式的结果
StringBuffer result = new StringBuffer();
try {
URL url = new URL(serverUrl);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//InputStream in = new FileInputStream(new File());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
line = new String(line.getBytes("UTF-8"));
result.append(line);
}
in.close(); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString(); }
}java.io.IOException: Server returned HTTP response code: 400 for URL: https://restapi.amap.com/v4/direction/truck?key=a6de56c08c9c6edf828e6469ac83dfc4&origin=121.161985,31.288490&destination=121.323141,31.296952&size=1&diu=imei&strategy=1&height=1.6&width=2.5&load=0.9&weight=10&axis=2&province=沪
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at cn.bulusi.MapTest.getResponse(MapTest.java:73)
at cn.bulusi.MapTest.getDistance(MapTest.java:53)
at cn.bulusi.MapTest.main(MapTest.java:28)
Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 0 of
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:972)
at net.sf.json.JSONObject._fromString(JSONObject.java:1201)
at net.sf.json.JSONObject.fromObject(JSONObject.java:165)
at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
at cn.bulusi.MapTest.getDistance(MapTest.java:54)
at cn.bulusi.MapTest.main(MapTest.java:28)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货