简体   繁体   English

Android Volley从onResponse问题得到回应

[英]Android Volley get response from onResponse issue

I have tried the method from Android Volley return value from response but I keep getting this error : com.box.ajian.box.Controllers.IOFunctions cannot be cast to com.box.ajian.box.Constants.VolleyCallback . 我已经尝试了从Android Volley返回值的方法,但我不断收到此错误:com.box.ajian.box.Controllers.IOFunctions无法转换为com.box.ajian.box.Constants.VolleyCallback。 What should I put in my casting area for it to work? 我应该在我的铸造区域放置什么才能工作? I am quite confused with tis volleycallback stuff. 我对tis volleycallback的东西很困惑。

This is my code. 这是我的代码。 Hope someone can help me 希望可以有人帮帮我

IOFunctions.java (Not an activity) IOFunctions.java(不是活动)

public void checkIfPersonExists(final Context context, final Person aPerson){
        StringRequest postRequest = new StringRequest(Request.Method.POST, USEREXISTS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("RESPONSE",response.toString());
                ((VolleyCallback)context).onSuccess(response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                params.put("email",aPerson.getEmail());
                params.put("phone",aPerson.getPhone());
                return params;
            }
        };
        Volley.newRequestQueue(context).add(postRequest);
    }

Functions.java (Not an activity) Functions.java(不是活动)

public Boolean register(Person aPerson,Context context){
        ioFunctions.checkIfPersonExists(context,aPerson);
        new VolleyCallback(){
            @Override
            public void onSuccess(String result) {
                if(result.equals(false)){
                    Log.d("HELLO","HELLO");
                }
            }
        };
    return false;
}

Register.java (Activity) Register.java(活动)

 functions.register(new Person(Firstname,Lastname,functions.dateFormatter(DateofBirth),Email,Password,Phone),Register.this);

Interface 接口

public interface VolleyCallback {
    void onSuccess(String result);
}

The activity register calls the functions.java which then calls iofunctions.java. 活动寄存器调用functions.java,然后调用iofunctions.java。 I want iofunctions.java to return a true or false and so Im relying on volley. 我希望iofunctions.java返回true或false,所以我依靠凌空。

You do not need your own callback class. 您不需要自己的回调类。 Volley comes with it's own! Volley自带它!

Just add it to the parameters, pass it to the request. 只需将其添加到参数中,将其传递给请求即可。

public void checkIfPersonExists(final Context context, final Person aPerson, 
    Response.Listener<String> onResponse){ // <-- here
        StringRequest postRequest = new StringRequest(Request.Method.POST, USEREXISTS, 
            onResponse, // <--- here
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> params = new HashMap<>();
                    params.put("email",aPerson.getEmail());
                    params.put("phone",aPerson.getPhone());
                    return params;
                }
        };
    Volley.newRequestQueue(context).add(postRequest);
}

Then handle accordingly, but don't make this method return because Volley is asynchronous. 然后相应处理,但不要让这个方法返回,因为Volley是异步的。

public void register(Person aPerson,Context context){
    ioFunctions.checkIfPersonExists(context,aPerson, 
        new Response.Listener<String>(){
            @Override
            public void onResponse(String response) {
                boolean exists = !result.equals("false");
                if(!exists){
                    Log.d("Person exists","Nope!");
                    // Person does not exist... continue registration from here
                } else {
                    Log.d("Person exists", "That account exists!");
                }
            }
        };
    // Don't return here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM