从类实例中检索String属性

我有A和B类。 “ A”成为“ B”的实例,并调用修改String属性的方法。但是,调用后,“ A”中签入的属性为空。 谁能帮我吗?


Class B

private String Retro;
....
public boolean Login(final String user,final String pass){

        stringRequest = new StringRequest(Request.Method.POST,Url + strLogin,new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Retro = "Done";
                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                **Retro = error.getMessage();**
                Toast.makeText(context,"Error de acceso: "+Retro,Toast.LENGTH_LONG).show();  //error.getMessage()
            }

        }) {
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> parameters = new HashMap<>();
                parameters.put("identifier",user);
                parameters.put("password",pass);
                return parameters;
            }
        };

        // Add the request to the RequestQueue.
        requestQueue.add(stringRequest);
        return Retro == "Done";
    }

Class A

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportactionBar(toolbar);

        FloatingactionButton fab = findViewById(R.id.fab);
        fab.setOnClicklistener(new View.OnClicklistener() {
            @Override
            public void onClick(View view) {
                snackbar.make(view,"Replace with your own action",snackbar.LENGTH_LONG)
                        .setaction("action",null).show();
            }
        });

        accessDB = new StrapiDBaccess(this);

        signIn();
    }

    private void handleSignInResult(Task<GoogleSignInaccount> completedTask) {
        try {
            ...
            if (acct != null) {
                ....
                boolean result = accessDB.Login(parcelaccess.personEmail,"*******");

                tvAuth.setText(tvAuth.getText()+" "+**accessDB.getRetro()**); //
            }

        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            parcelaccess.signedIn = false;
            parcelaccess.personName = "Anónimo";
            Toast.makeText(this,"Error de autenticación con Google. Chequee que tiene internet e inténtelo de nuevo.",Toast.LENGTH_SHORT).show();

        }
    }
huanchenruomeng 回答:从类实例中检索String属性

我找到了疑问的答案。不可能通过字段,getter或任何直接方式来完成。实现成功或错误的接口是必要的。就是这样:

B级

public void Login(final String user,final String pass,final VolleyCallback callback){

    Retro = "";
    stringRequest = new StringRequest(Request.Method.POST,Url + strLogin,new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Display the first 500 characters of the response string.
                    ***callback.onSuccess("Done");***
                }
            },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Retro = Retro + String.copyValueOf(error.getMessage().toCharArray());
            ***callback.onError(Retro);***
              //error.getMessage()
        }

    }) {
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> parameters = new HashMap<>();
            parameters.put("identifier",user);
            parameters.put("password",pass);
            return parameters;
        }
    };

    // Add the request to the RequestQueue.
    requestQueue.add(stringRequest);
    Toast.makeText(context,"Error de acceso: "+ErrorCode,Toast.LENGTH_LONG).show();
}

A级

accessDB.Login(parcelAccess.personEmail,"************",new StrapiDBAccess.VolleyCallback(){
                @Override
                public void onSuccess(String result) {

                }

                @Override
                public void onError(String error)
                        //no pudo ingresar
                    }
                }
            });

好吧,我希望能帮助有这种麻烦的人

本文链接:https://www.f2er.com/3113903.html

大家都在问