0

I'm working on an application that plots map route based on inputs. Here's a sample input 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

Using httpget,httpresponse I plan to get the response from the URI in json. I'm having an issue with this link.

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 ...

I'm getting an error saying:-

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

So I thought it might have occurred since I did not encode the URI. So later I tried using

uri = URLEncoder.encode(uri, "UTF-8");

Then I've been getting a different error saying:-

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

Please help me in resolving this issue. Thanks in advance.

daemon54
  • 1,057
  • 3
  • 16
  • 36
  • I didn't count, but do you suppose that the | in "...true|38.839619..." is causing the issue? – Martin Jul 16 '13 at 21:14
  • I don't think that would have caused the problem as I do have an another code which does not have lat,lng but instead string addresses and it did work for that code. – daemon54 Jul 16 '13 at 21:19
  • I agree with Martin. I didn't think | was a legal URL character. Could be wrong but my editor agrees that | is at index 160 :) I skimmed through this, but it looked solid http://stackoverflow.com/questions/1856785/characters-allowed-in-a-url – kalelien Jul 16 '13 at 21:52
  • My original guess would have been the colon in "...optimize:true|38.83..." though (Thought this was a reserved URL char) – kalelien Jul 16 '13 at 22:01
  • Thanks to Martin and Thynk Apps. Your answer has got me the result. – daemon54 Jul 16 '13 at 22:02
  • I've been getting the second kind of error as I was trying to encode the colon too by using uri = uri.replace(":", "%3A"); which is not necessary at all. – daemon54 Jul 16 '13 at 22:02
  • Can you post the result as an answer so I could mark it as solved and give you the credit! – daemon54 Jul 16 '13 at 22:11
  • I'm having the same issue ! What was the solution??? – Tamar Cohen Mar 29 '14 at 10:08

0 Answers0