我想通过 HTTP 请求发送阿拉伯语(unicode)
当使用URLEncodedUtils.format(params, HTTP.UTF_8);
当单词是阿拉伯语时,它会给出 paramString
这个值
“%D8%A7%D9%84%D9%82%D8%A7%D9%87%D8%B1%D9%87”
这是我的代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
Log.d("Parser" , paramString);
url += "?" + paramString;
Log.d("parser" , url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
服务器代码
function getShippingAddress($email)
{
$customerAddress = Mage::getModel('customer/address');
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
$defaultShippingId = $customer->getDefaultShipping();
$customerAddress->load($defaultShippingId);
$response["success"] = 1;
$response["data"][0] = $customerAddress->getData('city');
$response["data"][1] = $customerAddress- >getData('street');
$response["data"][2] = $customerAddress->getData('telephone');
header('Content-Type: application/json; charset=utf-8');
return json_encode($response);
}
最佳答案
尝试使用 POST 发送参数。示例代码在这里:
private String sendHttpPost(String url, String msg)
throws Exception {
HttpPost post = new HttpPost(url);
StringEntity stringEntity = new StringEntity(msg, "UTF-8");
post.setEntity(stringEntity);
return execute(post);
}
或者试试这个:
String unicodeUrl = URLEncoder.encode(url += "?" + paramString, "UTF-8");
HttpPost post = new HttpPost(unicodeUrl);
更新:
如果您的参数 =“Some_String_In_Arabic”,请尝试以下代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
//String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
String unicodeUrl = URLEncoder.encode(url += "?" + params, "UTF-8");
Log.d("URL" , unicodeUrl);
HttpGet httpGet = new HttpGet(unicodeUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
并在服务器端使用(PHP 的)urldecode 方法将字符串恢复为阿拉伯语形式。
关于java - 使用 http 请求 GET android 发送阿拉伯语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126762/