查询中的非法字符

查询中的非法字符

我正在尝试使用google地理编码api。我似乎无法回避这个问题,即应用程序由于“查询中的非法字符”而崩溃。
堆栈跟踪

    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)

这是我从另一篇标记为“已接受”的文章中复制粘贴的代码示例。
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();
    }

}

响应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"));

空间应该是+%20。一个人应该使用正确的编码,但对于一个空间来说这并不重要。

09-13 11:27