Volley sending request as utf-8

為{幸葍}努か 提交于 2019-12-11 02:35:18

问题


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.

Here is my code :

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

回答1:


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/



来源:https://stackoverflow.com/questions/28614048/volley-sending-request-as-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!