Volley sending request as utf-8

2019-07-20 09:30发布

问题:

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/