从http响应获取Json字符串()
问题描述:
我想知道是否有任何方法从http response.body()
中提取Json字符串。在我的response.body()
里面我有:{"er":"manualBlock"}
我想处理这个字符串而不必使用split方法。从http响应获取Json字符串()
编辑 我有这个至今:
String[] parts = response.body().string().split("-");
result = parts[0];
if (result != null && result.equals("{\"er\":\"manualBlock\"}")) {
throw new BlockeduserException("User blocked", null);
}
答
我设法创建这样一个类来解决我的问题:
public class BlockResponse {
public String er;
}
然后我用谷歌,GSON来处理一切通过这样做:
String serverResponse = response.body().string();
Gson gson = new Gson();
result = gson.fromJson(serverResponse, BlockResponse.class);
并为我所用的比较:
if (result != null && result.er.equals("manualBlock")) {
throw new BlockeduserException("User blocked", null);
}
你使用什么HTTP客户端? –
嗨,谢谢你的回答,我正在使用Retrofit。 –
另外,你可以粘贴你目前的代码吗? –