小编典典

java.lang.IllegalArgumentException:查询索引为59的非法字符

json

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

我收到以下错误消息:
java.lang.IllegalArgumentException:索引59处的查询中的非法字符:http
:
//maps.google.com/maps/api/geocode/json?
address=
Agram,Bengaluru,卡纳塔克邦,印度&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;

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

阅读 314

收藏
2020-07-27

共1个答案

小编典典

用于在将参数URLEncoder.encode()值放入URL字符串之前对address参数值进行编码"Agram, Bengaluru, Karnataka, India",以使其变为类似

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

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

浏览器会自动对地址栏中输入的字符串进行智能URL编码,因此这就是它在此处工作的原因。

2020-07-27