使用改造库

时间:2015-11-09 21:15:45

标签: android retrofit

我正在使用改造库来检查登录凭据我的包含响应的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方法具有用户输入的用户名和密码 ApiService

   public interface ApiService {

@FormUrlEncoded
@POST("/login/login.php/")
Call<User> validateUser(@Field("username") String username, @Field("password") String password);
 }

和checkLogin方法,我创建了Retrofit对象

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:at retrofit.ExecutorCallAdapterFactory $ ExecutorCallback $ 1.run(ExecutorCallAdapterFactory.java:86)   11-09 22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem   E / AndroidRuntime:at   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:at   android.app.ActivityThread.main(ActivityThread.java:5146)11-09   22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem   E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native   方法)11-09 22:51:06.152   21558-21558 / com.votingsystem.tsiro.votingsystem E / AndroidRuntime:
  at 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:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)11-09   22:51:06.152 21558-21558 / com.votingsystem.tsiro.votingsystem   E / AndroidRuntime:at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

最后我设法在深入研究Retrofit 2.0.0文档之后解决了这个问题。根据本指南https://futurestud.io/blog/retrofit-2-upgrade-guide-from-1-9/,HttpUrl.resolve()方法创建类似于的链接。之后,我删除了领先/来自部分网址@POST(&#34; /login/login.php /&#34;),它覆盖了我的BASE_URL的最后一部分,我将它添加到我的BASE_URL,一切都像它一样是期待。

答案 1 :(得分:0)

使用最新版本的改造,即2.0.0,它允许完整的网址支持,并通过此link

创建您的pojo