“非法字符”中的URL HTTPGET Android中获得双重编码(“Illegal Charac

2019-09-16 14:37发布

我想现在找到了解决这一整个晚上...

我写这从Web服务器请求数据的应用程序。 JSON格式的服务器的答案。 一切正常,当我进入像一个元音到我的应用程序时除外。

下面我想请求URL是http://example.com/?q= ,我搜索“耶格尔”

那么正确的调用会为h ++号码:?//example.com/ Q =Ĵ%C3%A4ger(对不起,加迹象,但垃圾邮件防护犯规让我正确地张贴。)

所以我现在的问题是:

当我给我的网址字符串编码或未编码到HTTPGET它总会导致doublee编码的URL。

请求到我的服务器,然后http://example.com/?q=J%25C3%25A4ger (它编码百分号),这导致服务器数据库中对于j搜索%C3%A4ger什么显然是错误的。

所以我的问题是我怎么能achive,如果用户输入“耶格尔”我的应用程序调用正确编码的网址是什么?

谢谢你的帮助!

这是目前使用的代码... IST或许是最差的想法,我有...

URI url = new URI("http", "//example.com/?q=" + ((EditText)findViewById(R.id.input)).getText().toString(), null);
Log.v("MyLogTag", "API Request: " +  url);
HttpGet httpGetRequest = new HttpGet(url);

// Execute the request in the client
HttpResponse httpResponse;
httpResponse = defaultClient.execute(httpGetRequest);

Answer 1:

更新:对不起, HttpParams并不意味着对请求参数,但对于配置 HttpClient

在Android上,您可能需要使用Uri.Builder ,如建议在这个其他苏答案 :

Uri uri = new Uri.Builder()
    .scheme("http")
    .authority("example.com")
    .path("someservlet")
    .appendQueryParameter("param1", foo)
    .appendQueryParameter("param2", bar)
    .build();

HttpGet request = new HttpGet(uri.toString());

// This looks very tempting but does NOT set request parameters
// but just HttpClient configuration parameters:
// HttpParams params = new BasicHttpParams();
// params.setParameter("q", query);
// request.setParams(params);

HttpResponse response = defaultClient.execute(request);
String json = EntityUtils.toString(response.getEntity());

安卓之外,最好的办法是手工打造的查询字符串(与所有编码的麻烦),或发现类似Android的东西Uri.Builder




文章来源: “Illegal Characters” in URL for HttpGet in Android get double-encoded