8

What i am doing: I am trying to make a reverse geocoding in android

I am getting error as:: java.lang.IllegalArgumentException: Illegal character in query at index 59: http://maps.google.com/maps/api/geocode/json?address=Agram, Bengaluru, Karnataka, India&sensor=false

NOte: that request gets a json response in browser but not from my class below

This line is giving this error::

HttpGet httpget = new HttpGet(url);

JSONfunctions.java

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}
3
  • 1
    Consider URL encoding your URL parameter values. Commented Nov 25, 2014 at 12:32
  • @laalto ... Can you please show as an answer, i am new to make encoding requests ! Commented Nov 25, 2014 at 12:33
  • possible duplicate of Java URL encoding Commented Nov 25, 2014 at 12:41

3 Answers 3

15

Use URLEncoder.encode() to encode the value of your address parameter "Agram, Bengaluru, Karnataka, India" before putting it in the URL string so that it becomes something like

http://maps.google.com/maps/api/geocode/json?address=Agram,+Bengaluru,+Karnataka,+India&sensor=false

i.e. spaces changed to + and other special octets represented as %xx.

Browsers do smart URL encoding for strings entered in the address bar automatically so that's why it works there.

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

3 Comments

When i try that in the respone i am getting as java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null,
Don't encode the entire URL, just the value of the parameter that is part of the URL.
laalto that was helpful, thnx.
6

Build your url like,

final StringBuilder request = new StringBuilder(
        "http://maps.googleapis.com/maps/api/geocode/json?sensor=false");
request.append("&language=").append(Locale.getDefault().getLanguage());
request.append("&address=").append(
        URLEncoder.encode(locationName, "UTF-8"));

Comments

2

I am using httpclient 4.3.3

String messagestr = "Welcome to Moqui World";
String url="http://my.example.com/api/sendhttp.phpauthkey="+URLEncoder.encode("17djssnvndkfjb110d3","UTF-8")+"&mobiles=91"+URLEncoder.encode(contactNumber,"UTF-8")+"&message="+URLEncoder.encode(messagestr,"UTF8")+"&sender="+URLEncoder.encode("WMOQUI","UTF-8")+"&route=4";

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

It's working fine for me. I hope this may help you.

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.