我编写了一个Class来使用Volley执行网络操作,并在该方法中传递回调。 代码如下
public class MyNetUtil {
public interface MyNetCallbacks {
public void onResponse(String response);
public void onErrorResponse(VolleyError error);
public void onErrorResponseData(ErrorResponseData error);
public void onResponseCode(int code);
}
private void request(int method, String url, final Map<String, String> headMap, final byte[] bodyContent, final MyNetCallbacks myNetCallbacks) {
Log.d(TAG, " Getting url : " + url);
final StringRequest strReq = new StringRequest(method, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response);
if (myNetCallbacks != null) {
myNetCallbacks.onResponse(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
int statusCode = -1;
if (error != null && error.networkResponse != null) {
statusCode = error.networkResponse.statusCode;
}
VolleyLog.d(TAG, "Error: " + error.getMessage());
ErrorResponseData errorResponseData = null;
NetworkResponse response = error.networkResponse;
if (response != null && response.data != null) {
String errorResponse = new String(response.data);
if (statusCode == -1) {
statusCode = response.statusCode;
}
errorResponseData = ErrorResponseData.getResponseData(errorResponse);
}
if (myNetCallbacks != null) {
myNetCallbacks.onErrorResponse(error);
if (statusCode != -1) {
myNetCallbacks.onResponseCode(statusCode);
}
myNetCallbacks.onErrorResponseData(errorResponseData);
}
Log.d(TAG, " inside onErrorResponse " + statusCode);
}
}) {
// protected Map<String, String> getParams() {
// return stringMap;
// }
@Override
public byte[] getBody() throws AuthFailureError {
Log.d(TAG, "Body : " + new String(bodyContent));
return bodyContent;
}
@Override
public String getBodyContentType() {
return CONTENT_TYPE_APPLICATION_JSON;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
Log.d(TAG, " inside parseNetworkResponse, responseCode : " + response.statusCode);
if (myNetCallbacks != null) {
myNetCallbacks.onResponseCode(response.statusCode);
}
return super.parseNetworkResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", CONTENT_TYPE_APPLICATION_JSON);
headers.put("Accept", "application/api.ryder.v2");
headers.put("Authentication-Token", getAuthToken());
if (headMap != null) {
for (Map.Entry<String, String> entry : headMap.entrySet()) {
headers.put(entry.getKey() + "", entry.getValue() + "");
}
}
Log.d(TAG, " Header ");
Util.displayMap(TAG, headers);
return headers;
}
};
BaseApplication.getInstance().addToRequestQueue(strReq, "");
}
}
在我的活动中,我将其用作
private MyNetUtil mMyNetUtil = new MyNetUtil();
mMyNetUtil.request(Request.Method.DELETE, url, headMap, bodyContent, new MyNetUtil.MyNetCallbacks() {
@Override
public void onResponse(String response) {
showProgress(false);
}
@Override
public void onErrorResponse(VolleyError error) {
showProgress(false);
}
@Override
public void onErrorResponseData(ErrorResponseData error) {
showProgress(false);
}
@Override
public void onResponseCode(int code) {
showProgress(false);
setInfoMessage(R.string.alert__lbl__your_account_was_deleted_successfully);
Log.d(TAG, "onResponseCode " + code);
if (code == MyNetUtil.CODE_204) {
Log.d(TAG, "FINISH --------- " + code);
DataUser.deleteUserFromPref();
Toast.makeText(mContext, R.string.alert__lbl__your_account_was_deleted_successfully, Toast.LENGTH_SHORT).show();
ActivityProfile.this.finish();
return;
} else {
setErrorMessage(R.string.server_error);
}
}
});
控制将持续到ActivityProfile.this.finish();
但活动未获得finished
我试过onBackPressed也没用。
但是当我打电话给ActivityProfile.this.finish()
一边回调它就完成了
可能是什么问题,请帮帮我