我是云端点的新手。我在Cloud EndPoints中使用Google Map API
获取地理信息。我需要解码到JSON String
才能获得国家/地区名称,administrator_area_level_1和administrator_area_level_2
这是我的代码。
EndPoints.java
@ApiMethod(name = "getLocation", path = "getLocation")
public StringResponse getlocation(@Named("location") String location) {
GeoLocation geoLocation = new GeoLocation();
geoLocation.getCounty();
geoLocation.getAreaLevel1();
geoLocation.getAreaLevel2();
// do some other staff with these values
return new StringResponse(// some args);
}
GeoLocation.java
public class GeoLocation {
String out = "";
public GeoLocation() {
try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true");
// making connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
// Reading data's from url
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
// System.out.println(output);
out += output;
}
// How can I decode JSON String(out)
conn.disconnect();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getOut() {
return out;
}
}
我已经使用Gson
库在 Android 中工作了,我可以使用这个吗?还是有任何直接的方法来解码它?还是其他更好的方法?我需要在 pom.xml 中添加dependency
我正在使用maven。
答案 0 :(得分:1)
您可以看到this link了解gson依赖关系或使用最新版本:
> process.binding('util').getPromiseDetails(Promise.resolve({data: [1,2,3]}));
[ 1, { data: [ 1, 2, 3 ] } ]
> process.binding('util').getPromiseDetails(Promise.reject(new Error('no')));
[ 2, Error: no ]
> process.binding('util').getPromiseDetails(new Promise((resolve) => {}));
[ 0, <1 empty item> ]
并使用相同的this post:
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>