Android截击从响应返回值
如何从onResponse中获取响应以便返回它?我发现了这一点,但我真的不知道如何为我实现它 我希望有一个更新的方法或更容易的东西,我可能会错过 这是我的密码Android截击从响应返回值,android,android-volley,Android,Android Volley,如何从onResponse中获取响应以便返回它?我发现了这一点,但我真的不知道如何为我实现它 我希望有一个更新的方法或更容易的东西,我可能会错过 这是我的密码 public Boolean checkIfPersonExists(Context context, final Person aPerson){ StringRequest postRequest = new StringRequest(Request.Method.POST, USEREXISTS, new Resp
public Boolean checkIfPersonExists(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());
}
}, 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);
return null;
}
public Boolean checkIfPersonExists(上下文、最终人物){
StringRequest postRequest=新的StringRequest(Request.Method.POST,USEREXISTS,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.d(“RESPONSE”,RESPONSE.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“email”,aPerson.getEmail());
参数put(“phone”,aPerson.getPhone());
返回参数;
}
};
newRequestQueue(上下文).add(postRequest);
返回null;
}
正如前面提到的,您需要实现接口并获得回调
创建一个像这样的新接口
public interface GeneralCallbacks {
void VolleyResponse(String data);
}
@Override
public void onResponse(String response) {
((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
aPerson.SomeFunction(); // This will call person's function
Log.d("RESPONSE",response.toString());
}
在活动类上实现接口
public class YourActivity implements ScoreboardCallback
{
@Override
public void VolleyResponse(String data)
{
//do stuff with data
}
}
最后把你的反应函数改成这样
public interface GeneralCallbacks {
void VolleyResponse(String data);
}
@Override
public void onResponse(String response) {
((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
aPerson.SomeFunction(); // This will call person's function
Log.d("RESPONSE",response.toString());
}
正如前面提到的,您需要实现接口并获得回调
创建一个像这样的新接口
public interface GeneralCallbacks {
void VolleyResponse(String data);
}
@Override
public void onResponse(String response) {
((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
aPerson.SomeFunction(); // This will call person's function
Log.d("RESPONSE",response.toString());
}
在活动类上实现接口
public class YourActivity implements ScoreboardCallback
{
@Override
public void VolleyResponse(String data)
{
//do stuff with data
}
}
最后把你的反应函数改成这样
public interface GeneralCallbacks {
void VolleyResponse(String data);
}
@Override
public void onResponse(String response) {
((GeneralCallbacks)context).VolleyResponse(response); // This will make a callback to activity.
aPerson.SomeFunction(); // This will call person's function
Log.d("RESPONSE",response.toString());
}
你将一无所获。但调用带有response
作为参数的函数。该函数处理。。。但是我不能调用另一个函数,因为checkIfPersonExists是从活动中调用的。checkIfPersonExists位于另一个类中。您应该将其实现为链接中描述的接口,但您不了解该接口。您将不返回任何内容。但调用带有response
作为参数的函数。该函数处理。。。但是我不能调用另一个函数,因为checkIfPersonExists是从活动中调用的。checkIfPersonExists位于另一个类中。您应该将其实现为链接中所述的接口,但您不了解该接口。很抱歉,请澄清一下,您能否向我展示在其他地方调用的代码示例?如果需要参数,我如何调用响应?我修复了代码中的一些错误。那么,您想打电话给aPerson还是回拨到activity?或者在哪里?很抱歉要澄清一下,你能给我一个在别处调用代码的例子吗?如果需要参数,我如何调用响应?我修复了代码中的一些错误。那么,您想打电话给aPerson还是回拨到activity?或者在哪里?