格式化返回数据
This commit is contained in:
@@ -19,6 +19,7 @@ import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static com.mogo.cloud.network.NetConstants.GEOFENCE_HOST;
|
||||
import static com.mogo.cloud.network.NetConstants.REALTIME_LOCATION_HOST;
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/21 12:26
|
||||
@@ -66,8 +67,8 @@ public class NetworkActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onNext(BaseData<V2XRoadDataRes> value) {
|
||||
tvResult.setText(new Gson().toJson(value));
|
||||
System.out.println(new Gson().toJson(value));
|
||||
tvResult.setText(formatJson(new Gson().toJson(value)));
|
||||
System.out.println(formatJson(new Gson().toJson(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,7 +86,7 @@ public class NetworkActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
public void queryHelpSignal(String sn) {
|
||||
apiService = RetrofitFactory.INSTANCE.getInstance("http://dzt-realtimeLocation.zhidaozhixing.com")
|
||||
apiService = RetrofitFactory.INSTANCE.getInstance(REALTIME_LOCATION_HOST)
|
||||
.create(ApiService.class);
|
||||
if (apiService != null) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
@@ -101,8 +102,8 @@ public class NetworkActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onNext(BaseData<V2XSeekHelpRes> value) {
|
||||
tvResult.setText(new Gson().toJson(value));
|
||||
System.out.println(new Gson().toJson(value));
|
||||
tvResult.setText(formatJson(new Gson().toJson(value)));
|
||||
System.out.println(formatJson(new Gson().toJson(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,4 +119,62 @@ public class NetworkActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
boolean isInQuotationMarks = false;
|
||||
for (int i = 0; i < jsonStr.length(); i++) {
|
||||
last = current;
|
||||
current = jsonStr.charAt(i);
|
||||
switch (current) {
|
||||
case '"':
|
||||
if (last != '\\') {
|
||||
isInQuotationMarks = !isInQuotationMarks;
|
||||
}
|
||||
sb.append(current);
|
||||
break;
|
||||
case '{':
|
||||
case '[':
|
||||
sb.append(current);
|
||||
if (!isInQuotationMarks) {
|
||||
sb.append('\n');
|
||||
indent++;
|
||||
addIndentBlank(sb, indent);
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
case ']':
|
||||
if (!isInQuotationMarks) {
|
||||
sb.append('\n');
|
||||
indent--;
|
||||
addIndentBlank(sb, indent);
|
||||
}
|
||||
sb.append(current);
|
||||
break;
|
||||
case ',':
|
||||
sb.append(current);
|
||||
if (last != '\\' && !isInQuotationMarks) {
|
||||
sb.append('\n');
|
||||
addIndentBlank(sb, indent);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sb.append(current);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void addIndentBlank(StringBuilder sb, int indent) {
|
||||
for (int i = 0; i < indent; i++) {
|
||||
sb.append('\t');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user