Java 如何在android中将不同的回调传递给同一个函数
我是android新手,目前正在尝试发出凌空发帖请求,并从API获得响应。我所做的是在响应成功时调用回调。如果我从一个类调用,比如说Java 如何在android中将不同的回调传递给同一个函数,java,android,callback,android-volley,generic-callback,Java,Android,Callback,Android Volley,Generic Callback,我是android新手,目前正在尝试发出凌空发帖请求,并从API获得响应。我所做的是在响应成功时调用回调。如果我从一个类调用,比如说MainActivitycallback方法,这个回调可以正常工作,但是如果我尝试从其他类调用,这个回调就不起作用。我试图将volleyAPIService中的回调参数设置为泛型,但未能成功。任何形式的帮助都是值得的 VolleyAPIService.java public class VolleyAPIService { public void voll
MainActivity
callback方法,这个回调可以正常工作,但是如果我尝试从其他类调用,这个回调就不起作用。我试图将volleyAPIService中的回调参数设置为泛型,但未能成功。任何形式的帮助都是值得的
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);
}
}
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);
}
}
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()
。我知道类型的参数回调不匹配,并试图使回调参数对这两个类都是泛型的,我想不出一种方法来实现这一点
任何类型的帮助都是值得赞赏的,请提前感谢。我建议使用一个单独的接口类,而不要将其保存在
类或活动中
所以像这样声明一个接口。创建一个单独的文件
public interface VolleyCallback {
void onSuccess(String result);
}
然后在VolleyAPIService
类中创建VolleyCallback
接口的public
实例,如下所示。从volleyPost
方法中删除该参数,以实现更清晰的实现
public class VolleyAPIService {
public VolleyCallback callback;
public void volleyPost(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);
}
}
对UserLogin
类执行相同的操作
如果在单个活动
或片段
中有多个API调用,您可能希望在VolleyAPIService
类中保留一个标志,并将其传递给回调函数,您可以检测在onSuccess
回调中得到的API响应
希望这是明确的。请随时提问 非常感谢@Reaz。上述解释很清楚,效果很好。再次非常感谢。很高兴知道我能帮上忙。非常欢迎。:)
public class MainActivity extends AppCompatActivity implements VolleyCallback {
@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();
// Assign the callback here to listen the response from the API service.
volleyAPIService.callback = this;
volleyAPIService.volleyPost(URL, params, MainActivity.this);
}
@Override
public void onSuccess(String result) {
// Handle the success or failure here
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();
}
}
}