我在其他课程中编写自定义Volley Android。但我对此有疑问。这是我的自定义排球课程:
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;
}
}
我在我的活动中这样打电话:
public class GeneralBusinessActivity extends AppCompatActivity implements CustomVolley.OnCallbackResponse {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
context= getApplicationContext();
customVolley = new CustomVolley(context);
String url = "https://sds.com/api/search/detail/business?id_business=" + s_id_business + "&" + Variabel.app_key + "="
+ Variabel.value_app_key;
queue_getDetailResultBusiness = customVolley.Rest(Request.Method.GET, url, null, "getDetailResultBusiness");
}
@Override
public void OnVolleyErrorResponse(String TAG, String response) {
Toast.makeText(GeneralBusinessActivity.this, response, Toast.LENGTH_SHORT).show();
}
@Override
public void onVolleySuccessResponse(String TAG, String response) {
Toast.makeText(GeneralBusinessActivity.this, response, Toast.LENGTH_SHORT).show();
}
}
但这不是回调onError或onSuccess ......那么如何解决呢?
答案 0 :(得分:1)
您必须分配对象,否则它将保持为空。您可以在CustomVolley类中添加setter。 E.g
public void setOnCallbackResponse(OnCallbackResponse l) {
OnCallbackResponse = l;
}
并在您的活动中执行:
customVolley.setOnCallbackResponse(this);
this
作为setOnCallbackResponse
的参数,因为GeneralBusinessActivity
正在您的案例中实现接口
答案 1 :(得分:0)
尝试更改此内容:
public CustomVolley(Context c)
{
_c=c;
}
到这个
public CustomVolley(Context c)
{
_c=c;
OnCallbackResponse = (OnCallbackResponse) c;
}