1. 程式人生 > >解決利用Okhttp+Retrofit 傳送網路請求得到json是unicode

解決利用Okhttp+Retrofit 傳送網路請求得到json是unicode

我們在用Okhttp和Retrofit傳送網路請求時,得到的json中漢子是unicode編碼,如圖

此時我們需要在Okhttp初始化的時候設定它的網路編輯器,同時將返回值的編碼改成UTF-8.

之前設定的OKhttp

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(1800, TimeUnit.SECONDS)
                .readTimeout(1800, TimeUnit.SECONDS)
                .writeTimeout(1800, TimeUnit.SECONDS)
                .addNetworkInterceptor(logging)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .header("is_app", "1")
                                .build();
                        return chain.proceed(request);
                    }
                }).build();

我們可以將HttpLoggingInterceptor裡面的Logger介面實現我們自己的一個方式,打印出我們想要的資料。那麼我們可以如下操作:

1.新建一個類HttpLogger,讓它實現HttpLoggingInterceptor的Logger

public class HttpLogger implements HttpLoggingInterceptor.Logger {
    private StringBuilder mMessage = new StringBuilder();
    private static final String TAG = "HttpLogger";
    @Override
    public void log(String message) {
        // 請求或者響應開始
        if (message.startsWith("--> POST")) {
            mMessage.setLength(0);
        }
        // 以{}或者[]形式的說明是響應結果的json資料,需要進行格式化
        if ((message.startsWith("{") && message.endsWith("}"))
                || (message.startsWith("[") && message.endsWith("]"))) {
            message = JsonUtil.formatJson(JsonUtil.decodeUnicode(message));
        }
        mMessage.append(message.concat("\n"));
        // 響應結束,列印整條日誌
        if (message.startsWith("<-- END HTTP")) {
            MyLogger.d(TAG,mMessage.toString());
        }
    }
}

這樣我們就實現了我們自己列印log的方式。

2.新建JsonUtil的工具類,看一下:

public class JsonUtil {
    /**
     * 格式化json字串
     *
     * @param jsonStr 需要格式化的json串
     * @return 格式化後的json串
     */
    public static String formatJson(String jsonStr) {
        if (null == jsonStr || "".equals(jsonStr)) return "";
        StringBuilder sb = new StringBuilder();
        char last = '\0';
        char current = '\0';
        int indent = 0;
        for (int i = 0; i < jsonStr.length(); i++) {
            last = current;
            current = jsonStr.charAt(i);
            //遇到{ [換行,且下一行縮排
            switch (current) {
                case '{':
                case '[':
                    sb.append(current);
                    sb.append('\n');
                    indent++;
                    addIndentBlank(sb, indent);
                    break;
                //遇到} ]換行,當前行縮排
                case '}':
                case ']':
                    sb.append('\n');
                    indent--;
                    addIndentBlank(sb, indent);
                    sb.append(current);
                    break;
                //遇到,換行
                case ',':
                    sb.append(current);
                    if (last != '\\') {
                        sb.append('\n');
                        addIndentBlank(sb, indent);
                    }
                    break;
                default:
                    sb.append(current);
            }
        }
        return sb.toString();
    }

    /**
     * 新增space
     *
     * @param sb
     * @param indent
     */
    private static void addIndentBlank(StringBuilder sb, int indent) {
        for (int i = 0; i < indent; i++) {
            sb.append('\t');
        }
    }
    /**
     * http 請求資料返回 json 中中文字元為 unicode 編碼轉漢字轉碼
     *
     * @param theString
     * @return 轉化後的結果.
     */
    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }

}

它有兩個方法:a 將打印出來的資料更清晰化 b 將unicode編碼改成utf-8

3.將新建的HttpLogger新增到網路請求攔截器中

        HttpLogger httpLogger = new HttpLogger();
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(httpLogger);
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(1800, TimeUnit.SECONDS)
                .readTimeout(1800, TimeUnit.SECONDS)
                .writeTimeout(1800, TimeUnit.SECONDS)
                .addNetworkInterceptor(logging)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .header("is_app", "1")
                                .build();
                        return chain.proceed(request);
                    }
                }).build();HttpLogger httpLogger = new HttpLogger();
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(httpLogger);
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(1800, TimeUnit.SECONDS)
                .readTimeout(1800, TimeUnit.SECONDS)
                .writeTimeout(1800, TimeUnit.SECONDS)
                .addNetworkInterceptor(logging)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .header("is_app", "1")
                                .build();
                        return chain.proceed(request);
                    }
                }).build();

藍色字型就是同上面比較新加的程式碼,黃色字型代表新增網路請求攔截器。

這樣打印出來的資料就很明朗了:

{
                                                           	"success":true,
                                                           	"description":"成功",
                                                           	"biz":{
                                                           		"goods_info":{
                                                           			"real_payment":"100.00",
                                                           			"goods_name":"針織衫",
                                                           			"mailcode":"3912691654392",
                                                           			"mailcode_company":"yunda",
                                                           			"status":100,
                                                           			"goods_images":"https://img.kaishiqianbao.com/2018-05-08/shop_goods5af1315d7fba6376811.jpeg"
                                                           		},
                                                           		"logistics":{
                                                           			"message":"ok",
                                                           			"nu":"3912691654392",
                                                           			"ischeck":"1",
                                                           			"condition":"F00",
                                                           			"com":"yunda",
                                                           			"status":"200",
                                                           			"state":"3",
                                                           			"data":[
                                                           				{
                                                           					"time":"2017-12-28 18:51:24",
                                                           					"ftime":"2017-12-28 18:51:24",
                                                           					"context":"[
                                                           						北京主城區公司昌平融澤嘉園服務部
                                                           					]快件已被 已簽收 簽收",
                                                           					"location":"北京主城區公司昌平融澤嘉園服務部"
                                                           				},
                                                           				{
                                                           					"time":"2017-12-28 18:46:54",
                                                           					"ftime":"2017-12-28 18:46:54",
                                                           					"context":"[
                                                           						北京主城區公司昌平融澤嘉園服務部
                                                           					]進行派件掃描;派送業務員:雷森浩;聯絡電話:17610460722",
                                                           					"location":"北京主城區公司昌平融澤嘉園服務部"
                                                           				}
                                                           			]
                                                           		}
                                                           	}
                                                           }