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

问题描述 投票:0回答:2

我想用 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);
}
java android android-asynctask
2个回答
3
投票

尝试使用 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 方法以阿拉伯语形式返回字符串。


0
投票
  static Future GetSearch(searching, language) async {
    try {
      Map<String, String> headervalue = {
        "Content-Type": "application/json",
        //'search': Uri.encodeFull(searching),
        //'search': searching,
        'search': "${utf8.encode(searching)}",
        'language': language
      };
      var url1 = Uri.parse(
          "${BaseUrl.baseurl}/apikey");
      var response = await http.get(url1, headers: headervalue);
      print('search' + searching); // print(response.body);
      print(headervalue);
      return response;
    } catch (e) {
      print(e);
      // return onError(e, "login/Users/login");
    }
  }

'搜索': "${utf8.encode(searching)}",

如果你得到 无法从“RequestInit”读取“headers”属性:字符串包含非 ISO-8859-1 代码点。

如果您使用多种语言进行搜索,如果您使用英语以外的语言,则会出现 ISO 错误,因此您必须将其编码到客户端并在服务器端解码以获得结果

© www.soinside.com 2019 - 2024. All rights reserved.