Android Volley:糟糕的网址编码

时间:2015-05-14 11:15:06

标签: android json google-maps google-maps-api-3 android-volley

我使用Volley库来执行请求。

当我向Google Maps v3提出简单的地址地理编码请求时,我收到INVALID_REQUEST或400错误。

问题出现在网址中: http://maps.googleapis.com/maps/api/geocode/json?address=52072,Aachen-Horbach Gzg& region = DEU& sensor = false

http://maps.googleapis.com/maps/api/geocode/json?address=362 35,Abertamy-HorníBlatná& region = ITA& sensor = false

当我使用Apache DefaultHttpClient的相同网址时,一切正常。

我如何通过网址:

    StringBuilder addressDepartureUrl  = new StringBuilder();
    if (order.getDepartureAddress().getStreet()!=null && !order.getDepartureAddress().getStreet().equalsIgnoreCase(""))
                        addressDepartureUrl.append(order.getDepartureAddress().getStreet() + ", ");
    if (order.getDepartureAddress().getHouseNumber()!=null && ! order.getDepartureAddress().getHouseNumber().equalsIgnoreCase(""))
                        addressDepartureUrl.append(order.getDepartureAddress().getHouseNumber() + ", ");
    if (order.getDepartureAddress().getZipCode()!=null && !order.getDepartureAddress().getZipCode().equalsIgnoreCase(""))
                        addressDepartureUrl.append(order.getDepartureAddress().getZipCode() + "," );
    if (order.getDepartureAddress().getCity()!=null && !order.getDepartureAddress().getCity().equalsIgnoreCase(""))
                        addressDepartureUrl.append(order.getDepartureAddress().getCity() );
    if (order.getDepartureAddress().getCountryCode()!=null && !order.getDepartureAddress().getCountryCode().equalsIgnoreCase(""))
                        addressDepartureUrl.append("&region="+order.getDepartureAddress().getCountryCode() );
addressDepartureUrl.append("&sensor=false");

   String finalDepartureUrl = Constants.URL_GEOCODING + addressDepartureUrl.toString();

 RequestQueue queue = Volley.newRequestQueue(getActivity());

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                        url.toString(), null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {
                                Log.i(TAG + " getGeocodingResults response", response.toString());
                                Log.i(TAG + " flag", Integer.toString(flag));
                                volleyResponse = response;
                                mVolleyResponse.onDataReceived(flag, response, order);
                            }
                        }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        // hide the progress dialog
                    }
                });
                queue.add(jsonObjReq);

How can I receive the same results as with the help of `Apache` library ? 

1 个答案:

答案 0 :(得分:2)

我找到了答案。这是编码。可行的解决方案:

g.SelectMany(i => i.itemList).ToList();

所以我们应该只编码参数,而不是整个查询。

if (order.getDestinationAddress().getStreet() != null && !order.getDestinationAddress().getStreet().equalsIgnoreCase("")) addressDestinationUrl.append(URLEncoder.encode(order.getDestinationAddress().getStreet(), "UTF-8") + ", "); if (order.getDestinationAddress().getHouseNumber() != null && !order.getDestinationAddress().getHouseNumber().equalsIgnoreCase("")) addressDestinationUrl.append(URLEncoder.encode(order.getDestinationAddress().getHouseNumber(), "UTF-8") + ", "); if (order.getDestinationAddress().getZipCode() != null && !order.getDestinationAddress().getZipCode().equalsIgnoreCase("")) addressDestinationUrl.append(URLEncoder.encode(order.getDepartureAddress().getZipCode(), "UTF-8") + ","); if (order.getDestinationAddress().getCity() != null && !order.getDestinationAddress().getCity().equalsIgnoreCase("")) addressDestinationUrl.append(URLEncoder.encode(order.getDepartureAddress().getCity(), "UTF-8")); if (order.getDestinationAddress().getCountryCode() != null && !order.getDestinationAddress().getCountryCode().equalsIgnoreCase("")) addressDestinationUrl.append("&region=" + order.getDestinationAddress().getCountryCode()); addressDestinationUrl.append("&sensor=false"); String finalDepartureUrl = Constants.URL_GEOCODING +addressDepartureUrl.toString(); &等符号不应编码!