gpt4 book ai didi

android - 改造 - 在将其解析为 json 之前从响应主体中删除一些无效字符

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:17 25 4
gpt4 key购买 nike

我有一个外部 Web 服务,它在响应正文中返回 json 但嵌套在括号中,如下所示:

({"door_x":"103994.001461","door_y":"98780.7862376", "distance":"53.3"})

使用这段代码:

class AddressInfo {
String door_x;
String door_y;
}

interface AddressWebService {
@GET("/reversegeocoding")
AddressInfo reverseGeocoding(@Query("x") double x, @Query("y") double y);
}

显然失败了。这是堆栈跟踪:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:377)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at com.something.$Proxy7.reverseGeocoding(Native Method)
at com.something.ReverseGeocodingService.getAddress(ReverseGeocodingService.java:24)
at com.something.LocationProvider$1.run(LocationProvider.java:77)
at java.lang.Thread.run(Thread.java:864)
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:362)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at com.something.$Proxy7.reverseGeocoding(Native Method)
            at com.something.ReverseGeocodingService.getAddress(ReverseGeocodingService.java:24)
            at com.something.LocationProvider$1.run(LocationProvider.java:77)
            at java.lang.Thread.run(Thread.java:864)
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:362)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at com.something.$Proxy7.reverseGeocoding(Native Method)
            at com.something.ReverseGeocodingService.getAddress(ReverseGeocodingService.java:24)
            at com.something.LocationProvider$1.run(LocationProvider.java:77)
            at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
            at com.google.gson.Gson.fromJson(Gson.java:803)
            at com.google.gson.Gson.fromJson(Gson.java:768)
            at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:362)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at com.something.$Proxy7.reverseGeocoding(Native Method)
            at com.something.ReverseGeocodingService.getAddress(ReverseGeocodingService.java:24)
            at com.something.LocationProvider$1.run(LocationProvider.java:77)
            at java.lang.Thread.run(Thread.java:864)

在解析 json 之前删除括号的最佳方法是什么?

最佳答案

Gson 将正文反序列化为类型对象之前,您可以轻松清除 GsonConverter 中的响应。

 public class CleanGsonConverter extends GsonConverter{

public CleanGsonConverter(Gson gson) {
super(gson);
}

public CleanGsonConverter(Gson gson, String encoding) {
super(gson, encoding);
}

@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
String dirty = toString(body);
String clean = dirty.replaceAll("(^\\(|\\)$)", "");
body = new JsonTypedInput(clean.getBytes(Charset.forName(HTTP.UTF_8)));
return super.fromBody(body, type);
}
private String toString(TypedInput body){
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(body.in()));
while ((line = br.readLine()) != null) {
sb.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();

}
};

JsonTypedInput:

   public class JsonTypedInput implements TypedInput{

private final byte[] mStringBytes;

JsonTypedInput(byte[] stringBytes) {
this.mStringBytes = stringBytes;
}


@Override
public String mimeType() {
return "application/json; charset=UTF-8";
}



@Override
public long length() {
return mStringBytes.length;
}

@Override
public InputStream in() throws IOException {
return new ByteArrayInputStream(mStringBytes);
}
}

在这里,我将 GsonConverter 子类化,以便在将响应转换为对象之前访问它。 JsonTypedOutput 用于在清除垃圾字符后保留响应的 mime 类型。

用法:

restAdapterBuilder.setConverter(new CleanGsonConverter(gson));

这要归咎于您的后端人员。 :)

关于android - 改造 - 在将其解析为 json 之前从响应主体中删除一些无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com