Java.lang.IllegalArgumentException: Invalid character in request at index 59

What I am doing: I am trying to do reverse geocoding in android

I am getting an error like: java.lang.IllegalArgumentException: Invalid character in request at index 59: http://maps.google.com/maps/api/geocode/json?address=Agram , Bangalore, Karnataka, India and sensor = false

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

This line gives 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


source to share


3 answers


Use URLEncoder.encode()

to encode your parameter value address

"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 will automatically encrypt URLs for strings entered in the address bar so that they work there.

+10


source


Create your url,



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"));

      

+5


source


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 works great for me. Hope this helps you.

+1


source







All Articles