Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 反向地理编码中的异常_Android_Geocode - Fatal编程技术网

Android 反向地理编码中的异常

Android 反向地理编码中的异常,android,geocode,Android,Geocode,我正在尝试使用htc desire c反转地理代码,运行应用程序时,其抛出服务不可用异常。但是GeoCoder.isPresent()正在返回“true”。请帮我找出问题所在 这是我的代码: public class MainActivity extends Activity { LocationManager lManager; String provider; Location location ; @SuppressLint("NewApi") @Override protected

我正在尝试使用htc desire c反转地理代码,运行应用程序时,其抛出服务不可用异常。但是GeoCoder.isPresent()正在返回“true”。请帮我找出问题所在

这是我的代码:

public class MainActivity extends Activity {

LocationManager lManager;
String provider;
 Location location ;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    provider = lManager.getBestProvider(criteria, false);
    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider
      location = lManager.getLastKnownLocation(provider);
    }

    boolean geoCoder = false;
    Geocoder geo = new Geocoder(this, Locale.getDefault());
     geoCoder = Geocoder.isPresent();
     System.out.println("GEO CODER : "+geoCoder);

     try {
        List<Address> address = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
        System.out.println("Size------------- "+address.size());

        /*Address addr = address.get(0);
        System.out.println("City "+addr.getLocality());*/
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("in here--------------");
        e.printStackTrace();
    }

}

@smk给了你一个有用的链接。为了避免你做大量的研究,我将试着简短地回答你。地理编码器并不总是返回值。您可以尝试在for循环中发送请求3次。我至少能回来一次。若并没有,那个么它们可能是连接问题,也可能是其他问题,比如服务器并没有回复您的请求

我也有一个while循环,但我过去最多尝试10次。有时,即使连接到internet,它也不会返回任何内容。然后,我每次都用更可靠的方法获取地址:

我曾经获取纬度和经度,然后请求google服务器,用一个JSOn对象回复,该对象包含关于位置坐标的各种信息。以下是函数:

public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
我这样称呼它:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
    location = ret.getJSONArray("results").getJSONObject(0);
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}
希望这有帮助。我也厌倦了依赖地理编码器。这对我有用。虽然它可能比地理编码器慢。您可以将URL中的坐标替换为当前的纬度和经度坐标。尝试在web浏览器中查看返回的JSON对象。您将看到如何提取地址字符串。请尝试阅读以下线程:


smk给了你一个有用的链接。为了避免你做大量的研究,我将试着简短地回答你。地理编码器并不总是返回值。您可以尝试在for循环中发送请求3次。我至少能回来一次。若并没有,那个么它们可能是连接问题,也可能是其他问题,比如服务器并没有回复您的请求

我也有一个while循环,但我过去最多尝试10次。有时,即使连接到internet,它也不会返回任何内容。然后,我每次都用更可靠的方法获取地址:

我曾经获取纬度和经度,然后请求google服务器,用一个JSOn对象回复,该对象包含关于位置坐标的各种信息。以下是函数:

public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
我这样称呼它:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
    location = ret.getJSONArray("results").getJSONObject(0);
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}
希望这有帮助。我也厌倦了依赖地理编码器。这对我有用。虽然它可能比地理编码器慢。您可以将URL中的坐标替换为当前的纬度和经度坐标。尝试在web浏览器中查看返回的JSON对象。您将看到如何提取地址字符串。请尝试阅读以下线程:


而且

质量很低:你用的是什么语言?你的密码在哪里?您尝试了什么?@AnilM,您在AndroidManifest.xml中授予了Internet权限吗?是的……我在AndroidManifest.xmlStacktrace logcat中授予了Internet和访问精细位置权限。请注意每种低质量:您使用的是什么语言?你的密码在哪里?您尝试了什么?@AnilM,您在AndroidManifest.xml中授予了Internet权限吗?是的……我在AndroidManifest.xmlStacktrace logcat中授予了Internet和访问精细位置权限,请回答Shobhit Puri!!我真的被GeoCoder打动了…将尝试实现您的答案:)没问题。我也被困了一天。所以我想稍微抬起头会很好。祝你一切顺利。此外,如果您发现Geocore问题的任何更好的解决方案,请发布。:)当然,如果我找到更好的解决方案,我会发帖:)嗨,Shobhit,我能够成功地检索到位置,但说只有在我们实施谷歌地图时才能使用url?正如我所说,获取位置需要一点时间。它是获取地址字符串的一种替代方法。每件事都有它的优点和缺点。谢谢你的回答Shobhit Puri!!我真的被GeoCoder打动了…将尝试实现您的答案:)没问题。我也被困了一天。所以我想稍微抬起头会很好。祝你一切顺利。此外,如果您发现Geocore问题的任何更好的解决方案,请发布。:)当然,如果我找到更好的解决方案,我会发帖:)嗨,Shobhit,我能够成功地检索到位置,但说只有在我们实施谷歌地图时才能使用url?正如我所说,获取位置需要一点时间。它是获取地址字符串的一种替代方法。每件事都有其利弊。