java如何在安卓中向同一个函数传递不同的回调
我是安卓新手,目前正在尝试发出凌空发帖请求,并从API获得响应。我所做的是在响应成功时调用回调。如果我从一个类调用,比如说MainActivity
回调方法,这个回调可以正常工作,但是如果我试图从其他类调用,这个回调就不起作用。我试图将volleyAPIService中的回调参数设置为泛型,但未能成功。任何形式的帮助都是值得的
截击发球。java
public class VolleyAPIService {
public void volleyPost(final MainActivity.VolleyCallback callback, String URL, Map<String, String> param, Context context) {
RequestQueue requestQueue = Volley.newRequestQueue(context);
final Map<String, String> params = param;
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
callback.onSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {;
return params;
}
};
requestQueue.add(stringRequest);
}
}
正如我之前所说的,我试图使volleyPost()的第一个参数更通用,以便从任何类调用这个特定的方法,但没有成功
主要活动。java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
companyLogin("abc", "123");
}
public interface VolleyCallback {
void onSuccess(String result);
}
public void companyLogin(String companyname, String password) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
final Map<String, String> params = new HashMap<String, String>();
params.put("name", companyname);
params.put("pwd", password);
Intent volley_service = new Intent(MainActivity.this, VolleyAPIService.class);
MainActivity.this.startService(volley_service);
VolleyAPIService volleyAPIService = new VolleyAPIService();
volleyAPIService.volleyPost(new VolleyCallback() {
@Override
public void onSuccess(String result) {
//do stuff here
Log.d("VOLLEY", "onSuccess: " + result);
if (!result.isEmpty()) {
Intent userLoginActivity = new Intent(MainActivity.this, UserLogin.class);
startActivity(userLoginActivity);
} else {
AlertDialog.Builder login_failed = new AlertDialog.Builder(MainActivity.this);
login_failed.setMessage("Login Failed, invalid credentials")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = login_failed.create();
alert.show();
}
}
}, URL, params, MainActivity.this);
}
}
我在MainActivity.java
中使用回调调用volleyPost()
用户登录。java
public class UserLogin extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
userLogin("xyz", "456", "1")
}
public interface VolleyCallback {
void onSuccess(String result);
}
public void userLogin(String username, String password, String id) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
final Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
params.put("compId", id);
Intent volley_service = new Intent(UserLogin.this, VolleyAPIService.class);
UserLogin.this.startService(volley_service);
VolleyAPIService volleyAPIService = new VolleyAPIService();
volleyAPIService.volleyPost(new VolleyCallback() {
@Override
public void onSuccess(String result) {
//do stuff here
Log.d("VOLLEY", "onSuccess: " + result);
if (!result.isEmpty()) {
Intent userLoginActivity = new Intent(UserLogin.this, HomePage.class);
startActivity(userLoginActivity);
} else {
AlertDialog.Builder login_failed = new AlertDialog.Builder(UserLogin.this);
login_failed.setMessage("Login Failed, invalid credentials")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = login_failed.create();
alert.show();
}
}
}, URL, params, UserLogin.this);
}
}
我也尝试从这个类中调用volleyPost()
。我知道类型的参数回调不匹配,并试图使回调参数对这两个类都通用,我想不出一种方法来实现这一点
任何形式的帮助都是值得感激的,请提前感谢
# 1 楼答案
我建议使用一个单独的接口类,而不必将其保存在
Class
或Activity
中所以声明一个这样的接口。创建一个单独的文件
然后在
VolleyAPIService
类中创建VolleyCallback
接口的public
实例,如下所示。从volleyPost
方法中删除参数以实现更清晰的实现现在,从
MainActivity
开始,实现您创建的接口,并重写回调函数,如下所示对你的
UserLogin
类做同样的事情如果在一个
Activity
或Fragment
中有多个API调用,您可能希望在VolleyAPIService
类中保留一个标志,并将其传递给回调函数,以便检测在onSuccess
回调中得到的API响应希望这是明确的。请随时提问