提交 349322fc 编写于 作者: J jessyan

http response json format

上级 66e5c630
......@@ -15,6 +15,8 @@ import okio.Buffer;
import okio.BufferedSource;
import timber.log.Timber;
import static com.jess.arms.utils.CharactorHandler.jsonFormat;
/**
* Created by jess on 7/1/16.
......@@ -88,7 +90,7 @@ public class RequestIntercept implements Interceptor {
}
Timber.tag("Result").w("Body------>" + bodyString);
Timber.tag("Result").w(jsonFormat(bodyString));
if (mHandler != null)//这里可以比客户端提前一步拿到服务器返回的结果,可以做一些操作,比如token超时,重新获取
return mHandler.onHttpResultResponse(bodyString,chain,originalResponse);
......
......@@ -3,6 +3,10 @@ package com.jess.arms.utils;
import android.text.InputFilter;
import android.text.Spanned;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -33,7 +37,6 @@ public class CharactorHandler {
/**
* 字符串转换成十六进制字符串
*
* @param String str 待转换的ASCII字符串
* @return String 每个Byte之间空格分隔,如: [61 6C 6B]
*/
public static String str2HexStr(String str) {
......@@ -51,4 +54,28 @@ public class CharactorHandler {
}
return sb.toString().trim();
}
/**
* json 格式化
* @param bodyString
* @return
*/
public static String jsonFormat(String bodyString) {
String message;
try {
if (bodyString.startsWith("{")) {
JSONObject jsonObject = new JSONObject(bodyString);
message = jsonObject.toString(4);
} else if (bodyString.startsWith("[")) {
JSONArray jsonArray = new JSONArray(bodyString);
message = jsonArray.toString(4);
} else {
message = bodyString;
}
} catch (JSONException e) {
message = bodyString;
}
return message;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册