Android Volley:错误的 URL 编码

Android Volley : bad urls encoding

我使用 Volley 库来执行请求。

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

问题发生在 url 中,例如: http://maps.googleapis.com/maps/api/geocode/json?address=52072,Aachen-Horbach Gzg®ion=DEU&sensor=false

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

当我将相同的 url 与 Apache DefaultHttpClient 一起使用时,一切正常。

我如何通过 url :

    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 ? 

我找到了答案。它在编码中。可行的解决方案:

      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();

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

不应该对 &= 等符号进行编码!