用http请求发送阿拉伯语单词GET android

时间:2016-03-21 09:07:00

标签: java android android-asynctask

我想用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);
}

1 个答案:

答案 0 :(得分:2)

尝试使用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);

<强>更新

如果您的params =“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();

并在服务器端使用urldecode方法(PHP)以阿拉伯语形式返回字符串。