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

fquxozlt  于 2023-04-10  发布在  Android
关注(0)|答案(2)|浏览(1414)

我想用HTTP请求发送阿拉伯语(unicode)
当使用URLEncodedUtils.format(params, HTTP.UTF_8);时,如果单词是阿拉伯语“%D8%A7%D9%84%D9%82%D8%A7%D9%87%D8%B1%D9%87”,则paramString将获得此值
这是我的代码:

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

7qhs6swi1#

尝试使用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的)以阿拉伯语形式获取字符串。

snvhrwxg

snvhrwxg2#

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

'search':“${utf8.encode(searching)}",
如果您正在获取Failed to read the 'headers' property from 'RequestInit':字符串包含非ISO-8859-1代码点。
如果您使用多种语言搜索,如果您使用英语以外的语言,则会出现ISO错误,因此您必须将其编码到客户端并在服务器端解码以获得结果

相关问题