Android - issue with encoding Arabic words

2019-07-25 00:35发布

问题:

I face problem in encoding Arabic response from web , I am using volley to call web service

my tries to fix this issue.

I created custom request then parse network response with utf-8 encoding when I log to check result it give me strange writing here is my log {"data":null,"msg":"لا يوجد تنبيهات","status":false} and here is my full class , I made some tries as well according to previous answer fixEncodingUnicode but all tries failed .

I appreciate any help thanks

import android.text.Html;
import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by mina on 7/28/2016.
 */

    public class myJsonObjectRequest extends JsonObjectRequest {


        public myJsonObjectRequest(int method, String url, String requestBody, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, requestBody, listener, errorListener);
        }

        public myJsonObjectRequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(url, listener, errorListener);
        }

        public myJsonObjectRequest(int method, String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, listener, errorListener);
        }

        public myJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, jsonRequest, listener, errorListener);
        }

        public myJsonObjectRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
            super(url, jsonRequest, listener, errorListener);
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

            try {
                response.headers.put("Content-Type",
                        response.headers.get("content-type"));
                String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

                Log.v("network_response", jsonString + "encoding "+ response.headers.get("content-type"));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }

        }


        private String DecodemyString(String msg) {
            try {
                return URLEncoder.encode(msg, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return "";
        }

        public static String fixEncodingUnicode(String response) {
            String str = "";
            try {
                str = new String(response.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }

            String decodedStr = Html.fromHtml(str).toString();
            return decodedStr;
        }
    }

回答1:

finally I figure nice solution to all who face encoding issue

First : make sure your android studio encoding utf-8 ,

File -> Settings -> Editor -> File Encodings choose utf-8 in encoding project

reference

Second: here is the trick , we need to discover what encoding did we receive , we can approach that in parseNetworkResponse method by called response.headers.get("content-type").

in my case I got charset:utf-8 that mean encoding in utf-8 but in fact its not , how we discover real encoding type here is site detect encoding https://2cyr.com/decode/ , just past text in site and made site handle Select encoding : auto detect

it will return pasted text with all encoding scroll down to find write encoding (you will see your writing in original language )

get source encoding: , displayed as:

in my case I found source encoding : utf-8 displayed as: windows-1254

this why I create method fix encoding issue

public static String fixEncodingUnicode(String response) {
            String str = "";
            try {
                                             // displayed as    desired encoding
                                                     ^                 ^
                                                     |                 |
                str = new String(response.getBytes("windows-1254"), "UTF-8");
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }

            String decodedStr = Html.fromHtml(str).toString();
            return decodedStr;
        }