Android Volley:错误的URL编码
问题描述:
我使用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("®ion="+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("®ion=" + order.getDestinationAddress().getCountryCode());
addressDestinationUrl.append("&sensor=false");
String finalDepartureUrl = Constants.URL_GEOCODING +addressDepartureUrl.toString();
所以我们应该只编码参数,而不是整个查询。
像&
和=
这样的符号不应该被编码!
为什么滥用没有解释? –