Java Android截击从onResponse问题获得响应
我已尝试了来自的方法,但不断出现以下错误:com.box.ajian.box.Controllers.IOFunctions无法强制转换为com.box.ajian.box.Constants.VolleyCallback。我应该在我的铸造区域放置什么才能让它工作?我对截击的东西很困惑 这是我的密码。希望有人能帮助我 IOFunctions.java(非活动) Register.java(活动) 接口Java Android截击从onResponse问题获得响应,java,android,android-volley,Java,Android,Android Volley,我已尝试了来自的方法,但不断出现以下错误:com.box.ajian.box.Controllers.IOFunctions无法强制转换为com.box.ajian.box.Constants.VolleyCallback。我应该在我的铸造区域放置什么才能让它工作?我对截击的东西很困惑 这是我的密码。希望有人能帮助我 IOFunctions.java(非活动) Register.java(活动) 接口 public interface VolleyCallback { void onSu
public interface VolleyCallback {
void onSuccess(String result);
}
活动寄存器调用functions.java,然后调用iofunctions.java。我希望iofunctions.java返回true或false,因此我依赖于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);
}
public void checkIfPersonExists(最终上下文、最终人物、,
Response.Listener onResponse){/您不需要自己的回调类。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);
}
public void checkIfPersonExists(最终上下文、最终人物、,
Response.Listener(onResponse){//谢谢回答。但是我想从函数中得到响应。我想将它返回到另一个函数。你不能使用return
。这就是我的观点。你必须使用回调。如果你需要register
函数的响应,那么你必须为onely添加另一个参数,不过,你应该返回结束作为“寄存器”的一部分执行“用户存在”功能处理。让客户端负责两个网络呼叫对电池来说都是不好的。我明白了。谢谢!如果你做了很多这样的请求,你可能会发现改型&RxJava是一个很好的组合感谢你的回答。但是我想从函数中得到响应。我想把它返回到另一个你不能使用的函数return代码>。这是我的观点。你必须使用回调。如果你需要从注册
函数中得到响应,那么你必须为oneReally添加另一个参数,不过,你应该让后端执行“用户存在”函数,作为“注册”的一部分处理。让客户端负责两个网络呼叫在电池上是不好的。我明白了。谢谢!如果你做了很多这样的请求,你可能会发现改型和RxJava是一个很好的组合
public interface VolleyCallback {
void onSuccess(String result);
}
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);
}
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
}