我正在尝试重构我的代码。所以我有以下几点:


主要LoginActivity

  private Boolean userauthenticated;

  onTryLogin(){  //login method executed after button click and validation

   JSONObject postparams = new JSONObject();
    try {
       postparams.put("username", login_username.getText());
       postparams.put("password", login_password.getText());
       postparams.put("client_secret", ApiHelper.PASSPORT_CLIENT_SECRET);
       postparams.put("grant_type", ApiHelper.PASSORT_GRANT_TYPE);
      postparams.put("client_id", ApiHelper.PASSPORT_CLIENT_ID);
    } catch (JSONException e) {
      e.printStackTrace();
   }

 AuthService loginhelper = new AuthService(this);
 userauthenticated = loginhelper.login(loginurl, postparams);

 if(userauthenticated){
    //this is always false never executed even when its true
  }
}



所以我的AuthService类有

public class AuthService {
     private Context ctx;
     private Boolean userloggedin = false;

      public AuthService(Context cnt){
        ctx = cnt;
     }

   public boolean  login(String loginurl, JSONObject loginparams){

      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
              (Request.Method.POST, loginurl, loginparams,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            //set access tokens here
                            Log.i("test","successifully"+response);
                            userloggedin = true;
                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    userloggedin = false;
                }
            }
            );


    // Access the RequestQueue through your singleton class.
    ApiSingleton strngle = new ApiSingleton(ctx);
    strngle.addToRequestQueue(jsonObjectRequest);

    return userloggedin;
  }

}


所以从上面的检查

 if(userauthenticated){
 }


始终返回false。即使在AuthService类onResponse中,我也将值设置为true。
我是Java的新手,我只是去看了Volley库。但是我不知道如何将值从authservice类传递到主登录类。

我要去哪里错了?

最佳答案

问题是,当您这样做时:

userauthenticated = loginhelper.login(loginurl, postparams);


您没有考虑login()函数是一个异步函数,因此它将始终返回false,因为这是此函数中返回值的默认值。您需要实现一个回调结构。就像是:

public class AuthService {
     private Context ctx;
     private OnLoginCallback callback;
     private Boolean userloggedin = false;

      public AuthService(Context cnt){
        ctx = cnt;
        callback =(OnLoginCallback) cnt;
     }

   public boolean  login(String loginurl, JSONObject loginparams){

      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
              (Request.Method.POST, loginurl, loginparams,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            //set access tokens here
                            Log.i("test","successifully"+response);
                            userloggedin = true;
                            callback.onLoginCallback(true);
                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    userloggedin = false;
                      callback.onLoginCallback(false);
                }
            }
            );


    // Access the RequestQueue through your singleton class.
    ApiSingleton strngle = new ApiSingleton(ctx);
    strngle.addToRequestQueue(jsonObjectRequest);

    return userloggedin;
  }

}


interface OnLoginCallback{
    void onLoginCallback(boolean loggedin);
}


然后在您的活动中实现接口OnLoginCallback并对onLoginCallback()方法做出反应。

07-24 16:07