"非法字符"在Android的网址为HTTPGET获得双恩codeD [英] "Illegal Characters" in URL for HttpGet in Android get double-encoded

查看:189
本文介绍了"非法字符"在Android的网址为HTTPGET获得双恩codeD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个解决的办法了整整一个晚上,现在...

我写这从Web服务器请求数据的应用程序。 JSON格式的服务器的答案。
一切都工作得很好,当我进入像一个变音符号到我的应用程序时除外。

在下面我想请求的URL为 http://example.com/?q= 我正在寻找耶格尔

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

所以我的问题是现在:

当我给我的网址字符串连接codeD或unen codeD到HTTPGET它总是会导致doublee恩codeD URL。

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

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

感谢您的帮助!

下面是目前使用code ... IST或许是最差的想法,我有...

  URI URL =新的URI(HTTP,//example.com/?q=+((的EditText)findViewById(R.id.input))。gettext的( )的ToString(),NULL);
Log.v(MyLogTag,API请求:+网址);
HTTPGET httpGetRequest =新HTTPGET(URL);//执行在客户端的请求
HTT presponse HTT presponse;
HTT presponse = defaultClient.execute(httpGetRequest);


解决方案

更​​新:对不起,的HttpParams 是不是为请求参数但配置 的HttpClient

在Android上,您可能需要使用 Uri.Builder ,像建议本其他SO回答

 开放的URI =新Uri.Builder()
    。方案(HTTP)
    .authority(example.com)
    。路径(someservlet)
    .appendQueryParameter(参数1,富)
    .appendQueryParameter(参数2,吧)
    。建立();HTTPGET请求=新HTTPGET(uri.toString());//这个看起来很诱人,但没有设置请求参数
//只是HttpClient的配置参数:
//的HttpParams PARAMS =新BasicHttpParams();
// params.setParameter(Q,查询);
// request.setParams(PARAMS);HTT presponse响应= defaultClient.execute(请求);
串JSON = EntityUtils.toString(response.getEntity());

的Andr​​oid之外,最好的办法是手动构建查询字符串(所有的编码麻烦),或发现类似Android的 Uri.Builder


I am trying to find a solution to this the whole evening now...

I write an app which requests data from a web server. The Server answers in JSON format. Everything works well except when I enter a umlaut like ä into my App.

In the following I assume the request URL is http://example.com/?q= and I am searching for "Jäger"

The correct call would then be h++p://example.com/?q=J%C3%A4ger (Sorry for plus-signs but the spam protection doesnt let me post it correctly.)

So my problem is now:

When I give my URL String encoded or unencoded over to HttpGet it will always result in a doublee-encoded URL.

The Request to my Server is then http://example.com/?q=J%25C3%25A4ger (It encodes the percent signs) which leads to the server searching in database for J%C3%A4ger what is obviously wrong.

So my question is how can I achive that if the user enters "Jäger" my app calls the correctly encoded URL?

Thanks for any help!

Here is the currently used code... Ist probably the worst possible idea I had...

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

解决方案

Update: Sorry, HttpParams isn't meant for request parameters but for configuring HttpClient.

On Android, you might want to use Uri.Builder, like suggested in this other SO answer:

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

Outside of Android, your best bet is building the query string manually (with all the encoding hassles) or finding something similar to Android's Uri.Builder.


这篇关于"非法字符"在Android的网址为HTTPGET获得双恩codeD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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