gpt4 book ai didi

android - 带有改造 API 的 MalformedJsonException?

转载 作者:IT老高 更新时间:2023-10-28 23:33:33 26 4
gpt4 key购买 nike

我需要向我的网络服务发送一个 json,json 是:

{
"Sala": {
"usuario": "%@",
"adversario": "%@",
"atualizacao": "%@",
"device": "%@",
"device_tipo": "ios"
}
}

。我正在尝试使用 Retrofit API 1.8 来做到这一点。当我尝试发送帖子时会引发异常。

异常(exception):

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 7 path $

我正在尝试这个

public class ChatObject {
private String usuario;
private String adversario;
private String atualizacao;
private String email;
private String device;
private String device_tipo;

改造界面

@POST("/WsChat/interacao.json")
public void onReceiveMessage(@Body ChatObject obj,
Callback<JsonElement> response);

实现

public void receiveMessage(){
///{\"Sala\":{\"usuario\":\"%@\",\"adversario\":\"%@\",\"atualizacao\":\"%@\",\"device\":\"%@\",\"device_tipo\":\"ios\"}}
ChatObject chatObject = new ChatObject(BatalhaConfigs.USUARIO_EMAIL,
BatalhaConfigs.ADVERSARIO_EMAIL,
new Date().toString(),
BatalhaConfigs.USUARIO_EMAIL,
AndroidReturnId.getAndroidId(),
"android");

RestAdapter adapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setRequestInterceptor(new CustomRequestInterceptor())
.setEndpoint(END_POINT)
.build();
ChatListener listener = adapter.create(ChatListener.class);
listener.onReceiveMessage(chatObject, new Callback<JsonElement>() {
@Override
public void success(JsonElement jsonElement, retrofit.client.Response response) {
Log.i("JSON ELEMENT->", jsonElement.toString());
}

@Override
public void failure(RetrofitError error) {
Log.i("FALHOU->", error.getLocalizedMessage());

}
});
}

最佳答案

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) 通常在某些字符格式不正确时抛出 JSON .异常消息本身建议使反序列化更加宽容。

但我建议你修复你的 JSON 并删除不需要的字符。

您应该扩展 GsonConverter 并覆盖 fromBody() 以使 Gson 从容错的 JsonReader 中读取。然后只需将其设置为您的 RestAdapter。如果没有抛出异常,这将尝试使用宽容的 JsonReader 反序列化然后关闭它。

public class LenientGsonConverter extends GsonConverter {
private Gson mGson;

public LenientGsonConverter(Gson gson) {
super(gson);
mGson = gson;
}

public LenientGsonConverter(Gson gson, String charset) {
super(gson, charset);
mGson = gson;
}

@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
boolean willCloseStream = false; // try to close the stream, if there is no exception thrown using tolerant JsonReader
try {
JsonReader jsonReader = new JsonReader(new InputStreamReader(body.in()));
jsonReader.setLenient(true);
Object o = mGson.fromJson(jsonReader,type);
willCloseStream = true;
return o;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(willCloseStream) {
closeStream(body);
}
}

return super.fromBody(body, type);
}

private void closeStream(TypedInput body){
try {
InputStream in = body.in();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

关于android - 带有改造 API 的 MalformedJsonException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27485346/

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