gpt4 book ai didi

java - 安卓截击。无法从有效的 Rest API 获取 JSON 响应

转载 作者:行者123 更新时间:2023-11-30 00:33:41 25 4
gpt4 key购买 nike

我在 POSTMAN 中测试了我的 REST 服务,它工作正常。我应该得到这样的东西:

{
"msg": "the user was successfully created"
...
}

但是在我调试的 android 应用程序中找不到响应。我错过了什么或者我做错了什么吗?谢谢!

private void registerUser(final String firstName, final String lastName,
final String email, final String password) {

pDialog.setMessage("Registering ...");
showDialog();

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonBody = new JSONObject();
jsonBody.put("first_name", firstName);
jsonBody.put("last_name", lastName);
jsonBody.put("email", email);
jsonBody.put("password", password);

final String requestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);
hideDialog();
...
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json";
}

@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);

}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}

在调试中, block

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);

}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}

显示:enter image description here

我没有 JSON 响应。

最佳答案

您没有看到任何 json 数据,因为您没有从您这边生成任何 json 响应。您在 Postman 上看到的响应来自 api 服务。您的程序已成功响应(即状态代码 201),这是您发送回客户端的唯一内容。

 if (response != null) {
responseString = String.valueOf(response.statusCode);

}

您必须操纵响应(在 parseNetworkResponse 模块中)并将您想要的相关信息转发给您的客户。

关于java - 安卓截击。无法从有效的 Rest API 获取 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43788974/

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