【发布时间】:2017-08-19 15:38:43
【问题描述】:
如果我写:
String str = response.errorBody().string();
String errors = Misc.get_errors(str);
一切正常(get_errors 是一个 final static 方法,它只能重新格式化 str)。
但是如果我直接写:
String errors = Misc.get_errors(response.errorBody().string());
失败了。
我的 Java 有点生疏了,我想知道为什么结果不一样。
编辑:
public final static String get_errors(String errors) {
StringBuilder sb = new StringBuilder();
try {
JSONObject jObjError = new JSONObject(errors);
Iterator<String> it = jObjError.keys();
while (it.hasNext()) {
sb.append(jObjError.get(it.next()).toString().replace("[\"", "").replace("\"]", ""));
sb.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
Callback<Joueur> callback = new Callback<Joueur>(){
@Override
public void onResponse(Call<Joueur> call, Response<Joueur> response){
// display message if http error(s)
if(!response.isSuccessful()){
try{
String raw_errors = response.errorBody().string();
String errors = Misc.get_errors(raw_errors);
Toast display = Toast.makeText(getApplicationContext(), errors, Toast.LENGTH_LONG);
display.show();
}
catch(Exception e){
e.printStackTrace();
}
}
// otherwise, launch new intent
else{
Intent myIntent = new Intent(Inscription_activity.this, Connexion_activity.class);
Inscription_activity.this.startActivity(myIntent);
}
}
【问题讨论】:
-
它是如何失败的?你能附上你的堆栈跟踪吗?
-
错误是什么?编译时间?
-
第二种情况,String为空。
-
你的尝试怎么样?你在读 response.errorBody() 两次吗?
-
我已经在第一篇文章中添加了整个代码。
标签: java android json retrofit