简体   繁体   中英

Google Geocoding API - Illegal character in query

I am trying to utilize the Google Geocoding API. I cannot seem to get around this issue where the app crashes due to an "Illegal character in query".

STACKTRACE

    10-18 17:13:57.986: E/AndroidRuntime(15822): FATAL EXCEPTION: main
10-18 17:13:57.986: E/AndroidRuntime(15822): Process: com.huddly.main, PID: 15822
10-18 17:13:57.986: E/AndroidRuntime(15822): java.lang.IllegalArgumentException: Illegal character in query at index 63: https://maps.googleapis.com/maps/api/geocode/json?address=Union Beach, NJ, United States&sensor=true
10-18 17:13:57.986: E/AndroidRuntime(15822):    at java.net.URI.create(URI.java:727)
10-18 17:13:57.986: E/AndroidRuntime(15822):    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
10-18 17:13:57.986: E/AndroidRuntime(15822):    at com.huddly.main.CreateSportEventFragmentActivity.getLatLongFromAddress(CreateSportEventFragmentActivity.java:289)
10-18 17:13:57.986: E/AndroidRuntime(15822):    at com.huddly.main.CreateSportEventFragmentActivity.onClick(CreateSportEventFragmentActivity.java:431)

Here is a sample of the code that I copy and pasted from another post here that was marked "Accepted".

private static final String GEOCODING_API_BASE = "https://maps.googleapis.com/maps/api/geocode";
private static final String OUT_JSON = "/json";




    public static void getLatLongFromAddress(String address) {
    StringBuilder sb = new StringBuilder(GEOCODING_API_BASE + OUT_JSON);
    sb.append("?address=" + address);
    //sb.append("&key=" + API_KEY);
    sb.append("&sensor=false");
    String uri = sb.toString();

    HttpGet httpGet = new HttpGet(uri);
    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) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());

        double lng = ((JSONArray) jsonObject.get("results"))
                .getJSONObject(0).getJSONObject("geometry")
                .getJSONObject("location").getDouble("lng");

        double lat = ((JSONArray) jsonObject.get("results"))
                .getJSONObject(0).getJSONObject("geometry")
                .getJSONObject("location").getDouble("lat");

        Log.e("latitude", "" + lat);
        Log.e("longitude", "" + lng);
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

STACK TRACE IN RESPONSE TO JOOP EGGEN

    10-18 18:27:33.307: E/AndroidRuntime(27343): FATAL EXCEPTION: main
10-18 18:27:33.307: E/AndroidRuntime(27343): Process: com.huddly.main, PID: 27343
10-18 18:27:33.307: E/AndroidRuntime(27343): android.os.NetworkOnMainThreadException
10-18 18:27:33.307: E/AndroidRuntime(27343):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
10-18 18:27:33.307: E/AndroidRuntime(27343):    at java.net.InetAddress.lookupHostByName(InetAddress.java:400)
10-18 18:27:33.307: E/AndroidRuntime(27343):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:251)
10-18 18:27:33.307: E/AndroidRuntime(27343):    at java.net.InetAddress.getAllByName(InetAddress.java:229)
10-18 18:27:33.307: E/AndroidRuntime(27343):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-18 18:27:33.307: E/AndroidRuntime(27343):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

    try {
        sb.append("?address=").append(URLEncoder.encode(address, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    };
sb.append("?address=").append(URLEncoder.encode(address, "UTF-8"));

The space should be + or %20 . One should use the right encoding, but for a space it does not really matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM