java.lang.IllegalArgumentException:索引59处的查询中存在非法字符 [英] java.lang.IllegalArgumentException: Illegal character in query at index 59

查看:1073
本文介绍了java.lang.IllegalArgumentException:索引59处的查询中存在非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么:
我试图在android中进行反向地理编码

错误为::
java.lang.IllegalArgumentException:索引59处的查询中存在非法字符: http://maps.google.com/maps/api/geocode/json?address=Agram ,印度卡纳塔克邦,班加罗尔& sensor = false


注意:该请求在浏览器中获得json响应,但不在我的课程下面获得。

这个错误::

  HttpGet httpget = new HttpGet(url); 






JSONfunctions.java

  public class JSONfunctions {

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

//从URL下载JSON数据
尝试{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);

HttpResponse响应= httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
$ b} catch(Exception e){
Log.e(log_tag,http connection中的错误+ e.toString());


//将响应转换为字符串
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is,iso-8859-1 ),8);
StringBuilder sb = new StringBuilder();
String line = null; ((line = reader.readLine())!= null){
sb.append(line +\\\
);
while
}
is.close();
result = sb.toString();
} catch(Exception e){
Log.e(log_tag,Error conversion result+ e.toString());
}

尝试{

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

return jArray;
}
}


解决方案

URLEncoder.encode() 来编码您的地址参数Agram,Bengaluru ,印度卡纳塔克邦,然后将其放入URL字符串中,以便它变成类似于



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

即空格变为 + 和其他特殊的八位字节表示为%xx



浏览器会自动为在地址栏中输入的字符串进行智能网址编码,这就是为什么它可以在那里运行。


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;
    }
}

解决方案

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.

这篇关于java.lang.IllegalArgumentException:索引59处的查询中存在非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆