我正在使用改造库来获取检查登录凭据,我的包含响应的php文件如下
login.php
...
if ($user["invalid credentials"] != "true"){
$response["error"] = FALSE;
$response["id"] = $user["credentials"]["admin_id"];
$response["username"] = $user["credentials"]["username"];
$response["password"] = $user["credentials"]["password"];
$response["email"] = $user["credentials"]["email"];
echo json_encode($response);
}
else {
$response["error"] = TRUE;
$response["error_msg"] = "Τα στοιχεία εισόδου είναι λάθος. Παρακαλώ ξαναπροσπάθησε.";
echo json_encode($response);
}
...
POJO课
public class User {
private int id;
private String error, error_msg, username, email, password;
public String getError(){
return this.error;
}
public String getError_msg(){
return this.error_msg;
}
public int getId(){
return this.id;
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public String getEmail(){
return this.email;
}
}
我的api服务接口,其中validateUser方法具有用户输入的用户名和密码
Api服务
public interface ApiService {
@FormUrlEncoded
@POST("/login/login.php/")
Call<User> validateUser(@Field("username") String username, @Field("password") String password);
}
和我创建Retrofit对象的checkLogin方法
private void checkLogin(String username, String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<User> call = apiService.validateUser(username, password);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(retrofit.Response<User> response, Retrofit retrofit) {
String email = response.body().getEmail();
Log.d(debugTag, "email: " + email);
}
@Override
public void onFailure(Throwable t) {
}
});
}
当我尝试获取用户的电子邮件时(如果凭据有效),将发生错误。我收到一个空指针异常...我的POJO类有问题吗?
致命异常:主要
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:进程:com.votingsystem.tsiro.votingsystem,PID:21558
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:java.lang.NullPointerException
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:at com.votingsystem.tsiro.fragments.LoginFragment $ 4.onResponse(LoginFragment.java:175)
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:在retrofit.ExecutorCallAdapterFactory $ ExecutorCallback $ 1.run(ExecutorCallAdapterFactory.java:86)
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于
android.os.Handler.handleCallback(Handler.java:733)11-09 22:51:06.152
21558-21558 / com.votingsystem.tsiro.votingsystem E / AndroidRuntime:
在android.os.Handler.dispatchMessage(Handler.java:95)11-09
22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于android.os.Looper.loop(Looper.java:136)11-09
22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于
android.app.ActivityThread.main(ActivityThread.java:5146)11-09
22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于java.lang.reflect.Method.invokeNative(本机
方法)11-09 22:51:06.152
21558-21558 / com.votingsystem.tsiro.votingsystem E / AndroidRuntime:
在java.lang.reflect.Method.invoke(Method.java:515)11-09 22:51:06.152
21558-21558 / com.votingsystem.tsiro.votingsystem E / AndroidRuntime:
在
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:732)
11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)11-09
22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem
E / AndroidRuntime:位于dalvik.system.NativeStart.main(本机方法)
最佳答案
最终,在深入研究Retrofit 2.0.0文档之后,我设法解决了该问题。根据本指南https://futurestud.io/blog/retrofit-2-upgrade-guide-from-1-9/,HttpUrl.resolve()方法创建类似于的链接。之后,我从覆盖BASE_URL最后一部分的部分url @POST(“ / login / login.php /”)中删除了开头的/,并将其添加到BASE_URL中,一切正常。