我正在为Android制作应用程序。我使用Google地图反向地理编码器对一组坐标进行反向地理编码,并解析返回的JSON以提取城市名称。但它不是像“纽约”这样的城市名称,而是返回com.example.geocoding_example.ParsingAsyncClass@418efd90(4025)
我做错了什么而不是真正的答案?感谢。
这是ParsingAsyncClass:
package com.example.gecoding_example;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class ParsingAsyncClass extends AsyncTask {
String cityName = null;
@Override
protected String doInBackground(Object... arg0)
{double latitude=40.7130;
double longitude=74.0135;
final AndroidHttpClient ANDROID_HTTP_CLIENT = AndroidHttpClient.newInstance(GecodingActivity.class.getName());
String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" +latitude + ","
+longitude + "&sensor=false&language=fr";
try
{
JSONObject googleMapResponse = new JSONObject(ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl),
new BasicResponseHandler()));
// many nested loops.. not great -> use expression instead
// loop among all results
JSONArray results = (JSONArray) googleMapResponse.get("results");
for (int i = 0; i < results.length(); i++)
{
// loop among all addresses within this result
JSONObject result = results.getJSONObject(i);
if (result.has("address_components"))
{
JSONArray addressComponents = result.getJSONArray("address_components");
// loop among all address component to find a 'locality' or 'sublocality'
for (int j = 0; j < addressComponents.length(); j++)
{
JSONObject addressComponent = addressComponents.getJSONObject(j);
if (result.has("types"))
{
JSONArray types = addressComponent.getJSONArray("types");
// search for locality and sublocality
for (int k = 0; k < types.length(); k++)
{
if ("locality".equals(types.getString(k)) && cityName == null)
{
if (addressComponent.has("long_name"))
{
cityName = addressComponent.getString("long_name");
}
else if (addressComponent.has("short_name"))
{
cityName = addressComponent.getString("short_name");
}
}
if ("sublocality".equals(types.getString(k)))
{
if (addressComponent.has("long_name"))
{
cityName = addressComponent.getString("long_name");
}
else if (addressComponent.has("short_name"))
{
cityName = addressComponent.getString("short_name");
}
}
}
if (cityName != null)
{
Log.d("Geoco== "+cityName, "tag");
return cityName;
}
}
}
}
}
}
catch (Exception e)
{
Log.d(e.toString(),"tag");
}
return null;
}
@Override
protected void onPostExecute(Object cityaName){
super.onPostExecute(cityaName);
String a;
a=cityaName.toString();
if (cityaName != null)
{
// Do something with cityName
Log.d("GeocoderHelpe123" +
"r"+cityaName, "tag");
}else{Log.d("GeocoderHelper232"+cityaName, "tag");}
}
}
答案 0 :(得分:1)
我花了一段时间才得到(很多Googleing和试错),但它确实有效! :)享受并感谢耶稣基督。
protected class RetrieveGoogleMapsCityName extends AsyncTask<URL, Integer, Void> {
protected AppCompatActivity a;
protected AlertDialog dialog;
private RetrieveGoogleMapsCityName(AppCompatActivity a) {
this.a = a;
}
@Override
protected void onPreExecute() {
this.dialog = new AlertDialog.Builder(this.a).create();
this.dialog.setCancelable(false);
this.dialog.setMessage("*****");
this.dialog.setTitle("*****");
this.dialog.show();
}
@Override
protected Void doInBackground(URL... urls) {
try {
final HttpURLConnection conn;
conn = (HttpURLConnection) urls[0].openConnection();
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),
"UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
if (!sb.toString().isEmpty()) {
JSONObject obj = new JSONObject(sb.toString());
JSONArray results = obj.getJSONArray("results");
JSONArray address_components = results.getJSONObject(0).getJSONArray("address_components");
for (int i = 0; i < address_components.length(); i++) {
//Log.v("JSONArray", address_components.getJSONObject(i).getString("long_name"));
//Log.v("JSONArrayTypes", address_components.getJSONObject(i).getJSONArray("types").getString(0));
if (address_components.getJSONObject(i).getJSONArray("types").getString(0).contentEquals("locality")) {
Log.v("JSONArray:i", i + " " + address_components.getJSONObject(i).getString("long_name"));
cityName = address_components.getJSONObject(i).getString("long_name");
}
if (address_components.getJSONObject(i).getJSONArray("types").getString(0).contentEquals("administrative_area_level_1")) {
stateLetters = address_components.getJSONObject(i).getString("short_name");
}
}
/*cityName =
obj.getJSONArray("results").getJSONObject(0)
.getJSONArray("address_components").getJSONObject(1).getString("long_name");
stateLetters = obj.getJSONArray("results").getJSONObject(0)
.getJSONArray("address_components").getJSONObject(4).getString("short_name");*/
}
if (conn != null) {
conn.disconnect();
}
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void v) {
this.dialog.dismiss();
hasLocation = true;
Log.v("onPostExecute", "You are located in " + cityName + ", " + stateLetters);
}
}
结果:您位于北卡罗来纳州夏洛特 结果2:您位于英国伦敦
答案 1 :(得分:0)
您正在调用类toString()
,但尚未实现返回您感兴趣的属性的toString()
方法。您看到的是对象的哈希码的十六进制表示形式
a=cityaName.toString();