gpt4 book ai didi

java - 改造 2.0 : getting response code 200 but not getting the desired data

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:29 30 4
gpt4 key购买 nike

Retrofit 2.0 的一个非常令人失望的特性是它没有准确指出它在解析响应时失败的位置。因此,在 postman 中,当我用相同的正文发送请求时,我得到的登录响应如下:

 {
"result": "success",
"response_code": 200,
"data": {
"id": "1",
"display_name": "admin",
"email": "payal@teckmovers.com",
"username": "admin",
"access_token": "8daa8e02ca432e51ae90912fbf63eeea"
}
}

但是,当我在 Retrofit 中使用完全相同的主体发出完全相同的请求时,我得到了一个非常奇怪的响应:{protocol=http/1.1, code=200, message=OK, url= http://192.168.0.52/evidya/wp-api/v1/user/login }.现在我已经解决了与上述问题相关的其他问题,但没有一个对我有用。请帮忙。我的代码:

改造API接口(interface):

public interface eVidyaApi {

@FormUrlEncoded
@POST("user/login")
Call<LoginResponse> loginUser(
@HeaderMap Map<String, String> headers,
@Field("email") String email,
@Field("password") String password
);
}

登录函数:

    public void login() {
Log.d(TAG, "Login");
if (!validate()) {
onLoginFailed();
return;
}

final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, R.style.MyDialogTheme);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating...");
progressDialog.show();

String email = _emailText.getText().toString();
String password = _passwordText.getText().toString();

Log.d(TAG, "login: "+email+" "+password);
// TODO: Implement your own authentication logic here.
Call<LoginResponse> loginResponseCall = evidya.loginUser(Common.getHeaders(), email, password);

loginResponseCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
progressDialog.dismiss();
if(!response.isSuccessful()){
Toast.makeText(LoginActivity.this, ""+response.message(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "onResponse: fail "+response.code());
return;
}

Log.d(TAG, "onResponse: success"+response.code()+" "+response);

if(response.body()!=null){
String content="";
// _loginButton.setEnabled(false);
LoginResponse loginResponse = response.body();
content += "code:"+ response.code();
content += "token:"+ loginResponse.getData().getAccessToken();
content += "result"+ loginResponse.getResult();
content += "result"+ loginResponse.getData().getDisplayName();
// onLoginSuccess();
Log.d(TAG, "onResponse: login res"+content);
} else {
Toast.makeText(LoginActivity.this, "Invalid response from server", Toast.LENGTH_SHORT).show();
}

}

@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, "Cannot fetch request", Toast.LENGTH_SHORT).show();

}
});
}

登录响应.java

package com.example.evidya.Retrofit.Model.LoginModel;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class LoginResponse {

@SerializedName("result")
@Expose
private String result;
@SerializedName("response_code")
@Expose
private Integer responseCode;
@SerializedName("data")
@Expose
private Data data;

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public Integer getResponseCode() {
return responseCode;
}

public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

}

数据.java

package com.example.evidya.Retrofit.Model.LoginModel;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Data {

@SerializedName("id")
@Expose
private String id;
@SerializedName("display_name")
@Expose
private String displayName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("username")
@Expose
private String username;
@SerializedName("access_token")
@Expose
private String accessToken;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

}

我的日志记录(ok hhttp),当点击带有错误信息的登录按钮时:

enter image description here

我的日志记录(ok hhttp),当点击带有正确详细信息的登录按钮时:

enter image description here解决方案:

基本上问题是我使用 Log.d(TAG, "onResponse: success"+response.code()+""+response); 检查 onresponse 回调中的响应.而我应该做的就是不要卡在那里并检查 loginResponse 对象的值(来自 LoginResponse loginResponse = response.body();)。因为 response.body 实际上是以对象的形式存储 reponse 的。这就是改造中的工作原理。

最佳答案

enter image description here

根据您的日志,API 调用正确。它也有反应。但问题是您的后端 API 身份验证失败。在您的网络服务上添加日志并检查。从应用程序方面来看,它工作正常。这不是 Retrofit 的问题。

使用以下内容更新您的 onResponse() 并运行应用程序。然后测试并让我知道你收到了什么信息。

if(response.body()!=null){
LoginResponse loginResponse = response.body();
String content="";
if (response.body().getResponseCode()==200){
content+= loginResponse.getData().getAccessToken();
content+= loginResponse.getData().getDisplayName();
content+= loginResponse.getData().getEmail();
content+= loginResponse.getData().getId();
content+= loginResponse.getData().getUsername();
}else{
content+=loginResponse.getData().getMsg();
}

Log.d(TAG, "onResponse: login res"+content);
} else {
Toast.makeText(LoginActivity.this, "Invalid response from server", Toast.LENGTH_SHORT).show();
}

Data.java 中的代码

 @SerializedName("msg")
@Expose
private String msg;
public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

关于java - 改造 2.0 : getting response code 200 but not getting the desired data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55467538/

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