如何将内部类(Response.Listener)中的变量值持久保存到外部?

时间:2015-12-15 12:14:21

标签: java android android-volley inner-classes gson

我在容器类中有一个名为facebookCheckEmail()(下面)的函数,我想更改fbresponse public void onResponse(FbCheckObject fbCheckObject) {}内容器类的变量值(facebookCheckEmail())我怎么做?

private void facebookCheckEmail( String email ) {



    fbCheckEmail_params = new HashMap<String, String>();
    fbCheckEmail_params.put("Email", email);

    final GsonRequest<FbCheckObject> gsonRequest = ApiRequest.gsonFacebookCheckEmailRequest
            (
                    new Response.Listener<FbCheckObject>() {
                        @Override
                        public void onResponse(FbCheckObject fbCheckObject) {


                            try {

                                System.out.println("Status = " + fbCheckObject.getStatus());

                                if (new String(fbCheckObject.getStatus()).equals("ok") == true) {

                                    fbresponse = "ok";

                                } else if(new String(fbCheckObject.getStatus()).equals("no") == true){

                                    fbresponse = "no";

                                }

                            } catch (Exception e) {

                                Log.d("Web Service Error", e.getMessage());
                                e.printStackTrace();
                            }
                        }
                    }
                    ,
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // Deal with the error here

                        }
                    },
                    fbCheckEmail_params
            );

    AppController.getInstance().addToRequestQueue(gsonRequest, TAG);

}

我的容器类是:

public class MainLoginActivity extends Activity {

  public String fbresponse = "";

      @Override
    protected void onCreate(Bundle savedInstanceState) {
     .......
    }

    ..........

    private void facebookCheckEmail( String email ) {
    ......
    }



}

注意:在&#34; @Override public void onResponse(FbCheckObject fbCheckObject) {}&#34;内,fbresponse值已成功分配给&#34; ok&#34;或&#34; no&#34;如果不成功,但如果我System.out.printIn fbresponse,我会得到&#34; null&#34;

1 个答案:

答案 0 :(得分:0)

做你想做的事并不是一个好的设计。您应该更改方法,以便返回状态:

    try {
      mDbManager.beginTransaction();
        mDbManager.copyToRealm(list);
        mDbManager.commitTransaction();
        Logger.v("Realm ", "Copied list to realm");
    } catch (Exception e) {
        Logger.e("Realm Something went wrong ", e);
    }

并将返回值设置为响应:

java.lang.IllegalStateException: Nested transactions are not allowed. Use commitTransaction() after each beginTransaction().