- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
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),当点击带有错误信息的登录按钮时:
我的日志记录(ok hhttp),当点击带有正确详细信息的登录按钮时:
基本上问题是我使用 Log.d(TAG, "onResponse: success"+response.code()+""+response);
检查 onresponse 回调中的响应.而我应该做的就是不要卡在那里并检查 loginResponse 对象的值(来自 LoginResponse loginResponse = response.body();
)。因为 response.body 实际上是以对象的形式存储 reponse 的。这就是改造中的工作原理。
最佳答案
根据您的日志,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/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!