如何将不同的回调传递给 android 中的同一个函数
How to pass different callbacks to same function in android
我是 android 的新手,我目前正在尝试发出 volley post 请求 并从 API 获得响应。我所做的是在响应成功时调用回调。如果我从单个 class 调用此回调,则此回调工作正常,例如,说 MainActivity
回调方法,但如果我尝试从其他 class 调用,则此回调无效。我试图将 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);
}
}
正如我之前所说,我试图使 volleyPost() 的第一个参数更通用,以便从任何 class 调用此特定方法,但未能成功。
MainActivity.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()
UserLogin.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);
}
}
我也尝试从这个 class 调用 volleyPost()
。我知道 类型的参数回调不匹配 并试图使回调参数对于 class 都是通用的,我想不出办法做到这一点。
非常感谢任何形式的帮助,在此先致谢。
我想建议有一个单独的接口 class,而不是将它放在 Class
或 Activity
中。
所以像这样声明一个接口。创建一个单独的文件。
public interface VolleyCallback {
void onSuccess(String result);
}
然后在您的 VolleyAPIService
class 中创建 public
接口的 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);
}
}
现在从您的 MainActivity
实现您创建的接口并重写回调函数,如下所示。
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();
}
}
}
为您的 UserLogin
class 做同样的事情。
如果在单个 Activity
或 Fragment
中有多个 API 调用,您可能希望在 VolleyAPIService
class 中保留一个标志并将其传递给回调函数,您可以检测到您在 onSuccess
回调中获得了哪个 API 响应。
希望这是清楚的。请随时提出任何问题。
我是 android 的新手,我目前正在尝试发出 volley post 请求 并从 API 获得响应。我所做的是在响应成功时调用回调。如果我从单个 class 调用此回调,则此回调工作正常,例如,说 MainActivity
回调方法,但如果我尝试从其他 class 调用,则此回调无效。我试图将 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);
}
}
正如我之前所说,我试图使 volleyPost() 的第一个参数更通用,以便从任何 class 调用此特定方法,但未能成功。
MainActivity.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()
UserLogin.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);
}
}
我也尝试从这个 class 调用 volleyPost()
。我知道 类型的参数回调不匹配 并试图使回调参数对于 class 都是通用的,我想不出办法做到这一点。
非常感谢任何形式的帮助,在此先致谢。
我想建议有一个单独的接口 class,而不是将它放在 Class
或 Activity
中。
所以像这样声明一个接口。创建一个单独的文件。
public interface VolleyCallback {
void onSuccess(String result);
}
然后在您的 VolleyAPIService
class 中创建 public
接口的 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);
}
}
现在从您的 MainActivity
实现您创建的接口并重写回调函数,如下所示。
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();
}
}
}
为您的 UserLogin
class 做同样的事情。
如果在单个 Activity
或 Fragment
中有多个 API 调用,您可能希望在 VolleyAPIService
class 中保留一个标志并将其传递给回调函数,您可以检测到您在 onSuccess
回调中获得了哪个 API 响应。
希望这是清楚的。请随时提出任何问题。