更新时间:2022-10-23 16:55:35
更新:对不起,的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());
的Android之外,***的办法是手动构建查询字符串(所有的编码麻烦),或发现类似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
.