我已尝试过Android Volley return value from response的方法,但我一直收到此错误:com.box.ajian.box.Controllers.IOFunctions无法转换为com.box.ajian.box.Constants.VolleyCallback。我应该在我的铸造区域放置什么才能工作?我对tis volleycallback的东西很困惑。
这是我的代码。希望有人能帮助我
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(非活动)
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(活动)
functions.register(new Person(Firstname,Lastname,functions.dateFormatter(DateofBirth),Email,Password,Phone),Register.this);
界面
public interface VolleyCallback {
void onSuccess(String result);
}
活动寄存器调用functions.java,然后调用iofunctions.java。我希望iofunctions.java返回true或false,所以我依靠凌空。
答案 0 :(得分:0)
您不需要自己的回调课程。 Volley自带了它!
只需将其添加到参数中,将其传递给请求即可。
changed_lst = ["a", ["CHANGED","c"], "d", ["e", ["f", "g", "CHANGED"]]]
然后相应处理,但不要让这个方法返回,因为Volley是异步的。
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);
}