gpt4 book ai didi

java - 如何解析来自改造请求的以下错误响应?

转载 作者:行者123 更新时间:2023-12-01 23:20:56 29 4
gpt4 key购买 nike

我正在尝试解析改造队列的错误主体。日志显示response.errorBody为

"{"response":["Image height must not exceed image width. Try a different image."]}"

这是我的代码:

call.enqueue(new Callback<EditBlogResponse>() {
@Override
public void onResponse(Call<EditBlogResponse> call, Response<EditBlogResponse> response) {
if(response.body() != null){
Log.d(TAG, "onEditResponse: " + response.body());
editBlogResponse.setValue(response.body().toString());
}else{
try {
Log.d(TAG, "onResponseError: " + response.errorBody().string());
JSONObject jsonObject = new JSONObject(response.errorBody().string());
JSONArray jsonArray = jsonObject.getJSONArray("response");
editBlogResponse.setValue(jsonArray.getString(0));
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}

}
}

@Override
public void onFailure(Call<EditBlogResponse> call, Throwable t) {
t.printStackTrace();
editBlogResponse.setValue("failure");
}
});

我收到系统错误

W/System.err: org.json.JSONException: End of input at character 0 of 
at org.json.JSONTokener.syntaxError(JSONTokener.java:460)
at org.json.JSONTokener.nextValue(JSONTokener.java:101)
at org.json.JSONObject.<init>(JSONObject.java:164)
at org.json.JSONObject.<init>(JSONObject.java:181)
at com.example.brandonblog.Repository.Repository$3.onResponse(Repository.java:176)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

关于我做错了什么有什么想法吗?

最佳答案

那么,在这种情况下,您的预期响应也在 onResponse() block 内。您必须在 block 中查找 4004xx 状态代码,如下所示:

  if(response.code() != 200) { 
Log.d(TAG, "onEditResponseCode: " + response.code());
Log.d(TAG, "onEditResponse: " + response.body());
editBlogResponse.setValue(response.body().toString());
} else {
Log.d(TAG, "onEditResponsecode: " + response.code());
Log.d(TAG, "onEditResponse: " + response.body().response.get(0));
//because your message is always at the 0th position of the 'response' list
// You don't need to use any JSONObject or JSONArray etc for these.
}

尝试让我知道它是否有效。

关于java - 如何解析来自改造请求的以下错误响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328045/

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