3

I am getting an error in http connection java.lang.IllegalArgumentException: Illegal character in query at index 76. Tried all the 3 examples.Unable to fix it .Tried url_encode but its not taking the components:locality part into consideration.How do I make this url work .Thanks in Advance.

BufferedReader in = null;

            HttpClient httpclient = new DefaultHttpClient();

            HttpGet request = new HttpGet();
            URI website = new URI("http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane|country=us|administrative_area:washington&address="+newText+"&sensor=false");
            request.setURI(website);
            HttpResponse response = httpclient.execute(request);
            in = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            String line = in.readLine();

            data = line;
            suggest = new ArrayList<String>();

And

                            String uri =("http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane|country=us|administrative_area:washington&address="+newText+"&sensor=false");

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

                            HttpClient hClient = new DefaultHttpClient();
                            HttpGet hGet = new HttpGet(uri);
                            ResponseHandler<String> rHandler = new BasicResponseHandler();
                            data = hClient.execute(hGet,rHandler);
                            suggest = new ArrayList<String>();

And

String urlstr="http://maps.googleapis.com/maps/api/geocode/json?";
                String str_parameters = "components=locality:Spokane|country=us|administrative_area:washington";
                String encodedparams = URLEncoder.encode(str_parameters,"UTF-8")+"&address="+URLEncoder.encode(newText, "UTF-8")+"&sensor="+URLEncoder.encode("false", "UTF-8");

                String str_finalurl=urlstr+encodedparams;

1 Answer 1

7

The character at 76 is |, which might be the cause of the issue. Have you tried replacing it with the hexadecimal format %7C, forming this: http://maps.googleapis.com/maps/api/geocode/json?components=locality:Spokane%7Ccountry=us%7Cadministrative_area:washington&address="+newText+"&sensor=false?

Also, is newText defined?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.