java illegalargumentexception(Android反向地理编码器)

时间:2014-05-08 05:51:56

标签: android http-post google-geocoder

我的应用程序在我调用下面的方法后崩溃了,我确定我可以从我的谷歌地图api获得纬度和经度,是因为googleurl ???错误发生在HttpPost httppost = new HttpPost(url) ;当我检查logcat时。

public String  getAddressUrl(Context context) {
            String responseText = null;
             //String latitude = "38.89";
             //String longitude  = "-77.03";
            String googleurl = "http://maps.google.com/maps/geo?";//url
            Log.v("TAG" , "Latitude is: " + mLocationClient.getLastLocation().getLatitude() + "Longitude is:" + mLocationClient.getLastLocation().getLongitude());
            StringBuilder sbuilder = new StringBuilder();
            sbuilder.append(googleurl);
            sbuilder.append("q = " + mLocationClient.getLastLocation().getLatitude() + "," + mLocationClient.getLastLocation().getLongitude());
            sbuilder.append("&output=responseText&sensor=true");
            String url = sbuilder.toString();

            Log.v("TAG", "url is: " + url);
            try {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse httpresponse = httpclient.execute(httppost);
                HttpEntity httpentity = httpresponse.getEntity();
                InputStream is = httpentity.getContent();
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                responseText = sb.toString();
            }
            catch(ClientProtocolException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return responseText;
        }

logcat的:

05-08 13:54:25.210: W/System.err(16238): java.lang.IllegalArgumentException: Illegal character in query at index 33: http://maps.google.com/maps/geo?q = 0.0,0.0&output = responseText&sensor = true
05-08 13:54:25.210: W/System.err(16238):    at java.net.URI.create(URI.java:727)
05-08 13:54:25.230: W/System.err(16238):    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
05-08 13:54:25.230: W/System.err(16238):    at com.example.syndessales.CheckInDetails.getAddressUrl(CheckInDetails.java:96)
05-08 13:54:25.230: W/System.err(16238):    at com.example.syndessales.CheckInDetails.onMyLocationButtonClick(CheckInDetails.java:208)
05-08 13:54:25.230: W/System.err(16238):    at com.google.android.gms.maps.GoogleMap$2.onMyLocationButtonClick(Unknown Source)
05-08 13:54:25.230: W/System.err(16238):    at com.google.android.gms.maps.internal.m$a.onTransact(Unknown Source)
05-08 13:54:25.230: W/System.err(16238):    at android.os.Binder.transact(Binder.java:297)
05-08 13:54:25.230: W/System.err(16238):    at euz.a(SourceFile:81)
05-08 13:54:25.230: W/System.err(16238):    at maps.e.bp.onClick(Unknown Source)
05-08 13:54:25.230: W/System.err(16238):    at android.view.View.performClick(View.java:3511)
05-08 13:54:25.230: W/System.err(16238):    at android.view.View$PerformClick.run(View.java:14105)
05-08 13:54:25.230: W/System.err(16238):    at android.os.Handler.handleCallback(Handler.java:605)
05-08 13:54:25.230: W/System.err(16238):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 13:54:25.230: W/System.err(16238):    at android.os.Looper.loop(Looper.java:137)
05-08 13:54:25.230: W/System.err(16238):    at android.app.ActivityThread.main(ActivityThread.java:4424)
05-08 13:54:25.230: W/System.err(16238):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 13:54:25.230: W/System.err(16238):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 13:54:25.230: W/System.err(16238):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
05-08 13:54:25.230: W/System.err(16238):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
05-08 13:54:25.230: W/System.err(16238):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您可以尝试更改:

sbuilder.append(googleurl);
sbuilder.append("q = " + mLocationClient.getLastLocation().getLatitude() + "," + mLocationClient.getLastLocation().getLongitude());
sbuilder.append("&output=responseText&sensor=true");
String url = sbuilder.toString();

为:

    googleurl+="q="+mLocationClient.getLastLocation().getLatitude()+","+mLocationClient.getLastLocation().getLongitude()+"&output=responseText&sensor=true&language=en-EN";

我已经在最后添加了语言参数,因为有时google会将格式化的地址返回给我其他语言:) 或者只是删除&#34;&#34;中的空格。括号。 我记得你不应该在&#34;&#34;中使用空格。引用,如果你想要它是一个网址。此外,您应该将此字符串编码为URL格式。如果要使用普通空格,则应将其更改为字符串中的%20。