0

我正在开发一个基于输入绘制地图路线的应用程序。这是一个示例输入 URI:-

http://maps.googleapis.com/maps/api/directions/json?origin=38.842794,-77.408132&destination=38.8685938,-77.2711787&region=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&region=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&region=en&sensor=true&waypoints=optimize:true|38.839619,-77.410309

请帮我解决这个问题。提前致谢。

4

0 回答 0