Android 接口未得到回调
我在另一节课上写自定义截击安卓。但我对这个有意见。这是我的自定义截击课程:Android 接口未得到回调,android,interface,Android,Interface,我在另一节课上写自定义截击安卓。但我对这个有意见。这是我的自定义截击课程: public class CustomVolley { private Context _c; public OnCallbackResponse OnCallbackResponse; public interface OnCallbackResponse { void OnVolleyErrorResponse(String TAG, String response);
public class CustomVolley {
private Context _c;
public OnCallbackResponse OnCallbackResponse;
public interface OnCallbackResponse {
void OnVolleyErrorResponse(String TAG, String response);
void onVolleySuccessResponse(String TAG, String response);
}
public CustomVolley(Context c) {
_c=c;
}
public RequestQueue Rest(int METHOD, String URL, final Map<String, String> jsonParams , final String TAG) {
Log.v("URL "+TAG, URL);
RequestQueue queue = Volley.newRequestQueue(_c);
StringRequest sr = new StringRequest(METHOD, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.v("onVolleySuccessResponse URL "+TAG, response);
if (OnCallbackResponse != null) {
OnCallbackResponse.onVolleySuccessResponse(TAG, response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
String resp="error";
if (response != null && response.data != null) {
resp = new String(response.data);
}
if (OnCallbackResponse != null) {
Log.v("OnVolleyErrorResponse URL "+TAG, resp);
OnCallbackResponse.OnVolleyErrorResponse(TAG, resp);
}
}
}) {
@Override
protected Map<String, String> getParams() {
return jsonParams;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(
Variabel.MY_SOCKET_TIMEOUT_MS_NEWS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
sr.setShouldCache(false);
sr.setTag(TAG);
queue.add(sr);
return queue;
}
}
但这不是一个错误或成功。。。那么如何解决它呢?您必须分配对象,否则它将保持为空。您可以在CustomVolley类中添加setter。例如
public void setOnCallbackResponse(OnCallbackResponse l) {
OnCallbackResponse = l;
}
在你的活动中:
customVolley.setOnCallbackResponse(this);
此
作为setOnCallbackResponse
的参数,因为GeneralBusinessActivity
正在您的案例中实现接口请尝试更改此:
public CustomVolley(Context c)
{
_c=c;
}
对此
public CustomVolley(Context c)
{
_c=c;
OnCallbackResponse = (OnCallbackResponse) c;
}
public CustomVolley(Context c)
{
_c=c;
OnCallbackResponse = (OnCallbackResponse) c;
}