2015-05-14 61 views
-2

我使用Volley庫來執行請求。Android Volley:錯誤的URL編碼

當我向Google Maps v3發出地址地址解析的簡單請求時,我收到INVALID_REQUEST或400錯誤。在像URL發生

問題: http://maps.googleapis.com/maps/api/geocode/json?address=52072,Aachen-Horbach GZG &區域=申&傳感器=假

http://maps.googleapis.com/maps/api/geocode/json?address=362 35,阿貝爾塔米-上布拉特納&區域= ITA &傳感器=假

當我使用相同的與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 ? 
+0

爲什麼濫用沒有解釋? –

回答

2

我找到了答案。這是在編碼。可行的解決方案:

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

所以我們應該只編碼參數,而不是整個查詢。

&=這樣的符號不應該被編碼!