截击发送请求为utf-8 [英] Volley sending request as utf-8

查看:124
本文介绍了截击发送请求为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Android上的 volley 库向Web发送请求,该请求的URL中包含一些 Arabic 字符,但是在php文件中却出现了问号而不是我的阿拉伯字母(??????),我尝试了人们在网络上说的解决方案,但所有这些解决方案都是尝试从网络读取数据,因为UTF-8不以UTF-8形式发送数据.

I am sending a request to web by volley library on android that contains some Arabic characters in the URL , But in the php file I got question marks instead of my arabic letters (??????) , I tried the solutions that people said on the net but all of them are try to read the data from web as UTF-8 not send data as UTF-8.

这是我的代码:

String url = "http://mywebsite.com/file.php?parameter=سلام"
JsonObjectRequest request = new JsonObjectRequest(Method.GET, url, null, new Listener<JSONObject>(){

    @Override
    public void onResponse(JSONObject response)
    {
        try
        {
            if(response.has("result") && response.getBoolean("result"))
            {
                prefs.edit().putBoolean(Launcher.REGISTERED_KEY, true).apply();

                Intent intent = new Intent(Subscribe.this, Home.class);

                Toast.makeText(Subscribe.this, R.string.register_success_message, Toast.LENGTH_LONG).show();

                startActivity(intent);
                Subscribe.this.finish();
            }
            else
            {
                msgBox.setText(R.string.subscribtion_error);
            }
        }
        catch(Exception e)
        {
            msgBox.setText(R.string.subscribtion_error);
            e.printStackTrace();
        }
    }
}, new ErrorListener(){
    @Override
    public void onErrorResponse(VolleyError error)
    {
        msgBox.setText(R.string.subscribtion_error);
        Log.e("", error.toString());
    }
}){
    /**
     * Passing some request headers
     * */
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
};

推荐答案

您需要先对自己的网址进行编码,然后才能在请求中使用它. 参见 http://istizada.com/understanding-arabic -url-uri-结构编码阿拉伯网站/

You need to encode your url before to use it in you request. See http://istizada.com/understanding-arabic-url-uri-structure-encoding-for-arabic-sites/

这篇关于截击发送请求为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆