gpt4 book ai didi

java - 如何使用 Volley 获取错误消息描述

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:21 26 4
gpt4 key购买 nike

我正在使用 Volley 库从 Android Java 向 C# 后端发送一个 http 请求。后端应用程序按预期使用错误代码和描述以及 StatusDescription 进行响应。我可以通过wireshark看到响应状态描述,但不知道如何在android端获取描述字符串。

    final JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.POST,
url,json,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
TextView mTextView = (TextView) findViewById(R.id.output);
print("Success");
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
TextView mTextView = (TextView) findViewById(R.id.output);
print("Failure (" + error.networkResponse.statusCode + ")");
//Trying to get the error description/response phrase here
}
}
);

这是处理请求的 C# 代码:

[WebInvoke(Method = "POST", UriTemplate = "users", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [操作合约] void addUser(字符串用户名,字符串名字,字符串姓氏,字符串电子邮件,字符串哈希) { Console.WriteLine(DateTime.Now + "Packet received");

        //Stores the response object that will be sent back to the android client
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
String description = "User added";
response.StatusCode = System.Net.HttpStatusCode.OK;

//Tries to add the new user
try
{
userTable.Insert(username,firstname,lastname,email,hash);
}
catch (SqlException e)
{
//Default response is a conflict
response.StatusCode = System.Net.HttpStatusCode.Conflict;

description = "Bad Request (" + e.Message + ")";

//Check what the conflict is
if (userTable.GetData().AsEnumerable().Any(row => username == row.Field<String>("username")))
{
description = "Username in use";
}
else if (userTable.GetData().AsEnumerable().Any(row => email == row.Field<String>("email")))
{
description = "Email address in use";
}
else
{
response.StatusCode = System.Net.HttpStatusCode.BadRequest;
}
}

//display and respond with the description
Console.WriteLine(description);
response.StatusDescription = description;
}

我浏览了其他人的问题,但似乎无法找到我正在寻找的答案。有人知道怎么做吗?我尝试过的许多方法都导致花括号为空,表示 JSON 主体为空。我正在尝试专门获取状态描述。

最佳答案

试试这个自定义方法:

public void parseVolleyError(VolleyError error) {
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
JSONObject data = new JSONObject(responseBody);
JSONArray errors = data.getJSONArray("errors");
JSONObject jsonMessage = errors.getJSONObject(0);
String message = jsonMessage.getString("message");
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
} catch (UnsupportedEncodingException errorr) {
}
}

它将显示带有请求错误消息的 toast。在你的截击请求中的 onErrorResponse 方法中调用它:

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
parseVolleyError(error);
}
}

关于java - 如何使用 Volley 获取错误消息描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841118/

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