我在另一堂课上写了定制的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..。那么如何解决呢?
发布于 2016-03-10 08:52:04
您必须分配对象,否则它将保持为空。您可以在CustomVolley类中添加一个setter。E.g
public void setOnCallbackResponse(OnCallbackResponse l) {
OnCallbackResponse = l;
}在你的活动中:
customVolley.setOnCallbackResponse(this);this作为setOnCallbackResponse的参数,因为在您的情况下,GeneralBusinessActivity正在实现接口
发布于 2016-03-10 09:07:42
试着改变这个:
public CustomVolley(Context c)
{
_c=c;
}到这个
public CustomVolley(Context c)
{
_c=c;
OnCallbackResponse = (OnCallbackResponse) c;
}https://stackoverflow.com/questions/35911540
复制相似问题