【问题标题】:java.lang.IllegalArgumentException: Illegal character in query at index 59java.lang.IllegalArgumentException:索引 59 处的查询中存在非法字符
【发布时间】:2015-01-23 11:08:07
【问题描述】:

我在做什么: 我正在尝试在 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;

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

【问题讨论】:

  • 考虑对您的 URL 参数值进行 URL 编码。
  • @laalto ...您能否作为答案显示,我是新来提出编码请求的!
  • Java URL encoding的可能重复

标签: java android json google-maps


【解决方案1】:

使用URLEncoder.encode()address 参数"Agram, Bengaluru, Karnataka, India" 的值进行编码,然后将其放入URL 字符串中,使其类似于

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

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

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

【讨论】:

  • 当我尝试响应时,我得到java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null,
  • 不要对整个 URL 进行编码,只对作为 URL 一部分的参数值进行编码。
  • laalto 很有帮助,谢谢。
【解决方案2】:

像这样构建你的网址,

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

【讨论】:

    【解决方案3】:

    我正在使用 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);
    

    它对我来说很好用。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多