我正在开发一个根据输入绘制地图路线的应用程序。这是一个示例输入URI: -
http://maps.googleapis.com/maps/api/directions/json?origin=38.842794,-77.408132&destination=38.8685938,-77.2711787®ion=en&sensor=true&waypoints=optimize:true|38.839619,-77.410309
使用httpget,httpresponse我计划从json中的URI获取响应。 我对此链接有疑问。
HttpGet httpget = new HttpGet(uri);
HttpResponse response;
try
{
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if(entity!=null)
{
InputStream instream = entity.getContent();
StreamToString st = new StreamToString();
result = st.convertStreamtoString(instream);
blah blah blah ...
我收到一个错误说: -
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 160: http://maps.googleapis.com/maps/api/directions/json?origin=38.842794,-77.408132&destination=38.8685938,-77.2711787®ion=en&sensor=true&waypoints=optimize:true|38.839619,-77.410309
所以我认为可能是因为我没有编码URI。所以后来我尝试使用
uri = URLEncoder.encode(uri, "UTF-8");
然后我得到了一个不同的错误说: -
java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://maps.googleapis.com/maps/api/directions/json?origin=38.842794,-77.408132&destination=38.8685938,-77.2711787®ion=en&sensor=true&waypoints=optimize:true|38.839619,-77.410309
请帮我解决这个问题。 提前谢谢。