【发布时间】:2022-01-19 14:54:01
【问题描述】:
现在我正在开发一个 Android SDK 项目,代码太旧,我尝试使用 okhttp3 从服务器收集数据。通过使用以下函数,我可以收集来自服务器的响应。
String json = requestGetString(url);
public String requestGetString(String uri) throws IOException {
Response res = null;
try {
res = getStringHttp(host+uri);
System.out.println(res.body().string());
return res.body().string();
}catch (NullPointerException e) {
//body = null ??
throw new IOException(e);
} catch (IOException e) {
throw new IOException(e);
}finally {
if(res != null){
try{
res.close();
}catch (NullPointerException e){
//body = null ??
throw new IOException(e);
}
}
}
}
我正在接收 Json,但我无法用我的数据类解析它。
喜欢下面的
AuthAppResponse result = parseJson(path, json, AuthAppResponse.class);
你可以看看我的解析器函数
private <TResponse> TResponse parseJson(String path, String json, Class<TResponse> responseClass) throws IOException
{
return parseJson(getGson(), path, json, responseClass);
}
还有这个,
private <TResponse> TResponse parseJson(Gson gson, String path, String json, Class<TResponse> responseClass) throws IOException
{
try
{
return gson.fromJson(json, responseClass);
}
catch (JsonSyntaxException e)
{
throw new IOException("The response from '" + path + "' failed to be parsed as JSON.\ncontent = " + json, e);
}
}
什么都没有发生。
并且具有最新代码库的相同 SDK 可以在相同代码下正常工作。任何人都有想法找出可能的解决方案。 请分享您的评论。
【问题讨论】:
标签: android json migration gson androidx