gpt4 book ai didi

android - 如何在android中取消单 Volley 请求?

转载 作者:行者123 更新时间:2023-12-05 00:21:52 25 4
gpt4 key购买 nike

我有一个 MainActivity其中添加了 Fragment发送服务器请求并包含 ListView滑动刷新,问题是当我向下滑动刷新时,如果我按下设备返回应用程序将同时发送服务器请求,此时应用程序崩溃。如何实现这个问题。

刷卡时刷新发送服务器请求的代码。

mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
/*Here check net connection avialable or not */
if (NetworkUtil.isConnected(getActivity())) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
int length = preferences.getInt("length", 0);
sz_LastCount = String.valueOf(length);// convert int value to string /////

Log.e(TAG, "Last Count::" + sz_LastCount);
Log.e(TAG, "Record count::" + sz_RecordCount);
/*Send Request to Server for more data */
loadmoreOnSwipe();
}
}, 3500);

} else {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No internet connection available", getActivity());
if (mSwipeRefresh.isRefreshing()) {

mSwipeRefresh.setRefreshing(false);
}
}
}
});

/*This method send request to server for more deals*/
private void loadmoreOnSwipe() {
try {
String json;
// 3. build jsonObject
final JSONObject jsonObject = new JSONObject();// making object of Jsons.
jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
jsonObject.put("pin", m_szEncryptedPassword);// put password
jsonObject.put("recordcount", sz_RecordCount);// put record count
jsonObject.put("lastcountvalue", sz_LastCount);// put last count

Log.d("CAppList:", sz_RecordCount);
Log.d("Capplist:", sz_LastCount);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();// convert Json object to string
Log.i(TAG, "Server Request:-" + json);

final String m_DealListingURL = "http://202.131.144.132:8080";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, "Server Response:-" + response);
if (mSwipeRefresh.isRefreshing()) {
mSwipeRefresh.setRefreshing(false);
}
try {
int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode"));

if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) {
// Select the last row so it will scroll into view...
JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
item = new CDealAppDatastorage();// object create of DealAppdatastorage
item.setM_szHeaderText(post.getString("dealname"));//getting deal name
item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
item.setM_szDealValue(post.getString("dealvalue"));

if (!s_oDataset.contains(item)) {
s_oDataset.add(item);
}
}
m_oAdapter.notifyDataSetChanged();
arrayCount = posts.length();// finding length of deals coming in response from server.
// read stored value from shared preference
int length = preferences.getInt("length", 0);

/*adding current array count with earlier saved value in shared preference*/
int accumulateLastCount = length + arrayCount;


/*Here we are saving deal length in shared preference*/
// save incremental length
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("length", accumulateLastCount);
editor.apply();
// int add = CLastCountData.getInstance().getS_szLastCount() + arrayCount;
//CLastCountData.getInstance().setS_szLastCount(add);
m_ListView.removeFooterView(mFooter);
m_ListView.setSelection(m_oAdapter.getCount() - posts.length());

}

if (nResultCodeFromServer == CStaticVar.m_kCONNECTION_LOST) {//server based conditions
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection Lost !", getActivity());
m_ListView.removeFooterView(mFooter);
} else if (nResultCodeFromServer == CStaticVar.m_kDEAL_NOT_FOUND) {// serevr based conditions .....
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No more deals available", getActivity());
//*Counting loading footer*/
if (m_ListView.getFooterViewsCount() != 0) {
m_ListView.removeFooterView(mFooter);
}
} else if (nResultCodeFromServer == CStaticVar.m_kTECHNICAL_FAILURE) {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Technical Failure", getActivity());
} else if (nResultCodeFromServer == CStaticVar.m_kALREADY_AVAIL_BENEFIT) {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "You have already avail the benefit of this deal", getActivity());
} else if (nResultCodeFromServer == CStaticVar.m_kTIMED_OUT) {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Timed Out", getActivity());
m_ListView.removeFooterView(mFooter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error:-" + error);
if (mSwipeRefresh.isRefreshing()) {
mSwipeRefresh.setRefreshing(false);
}
if (error instanceof TimeoutError) {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection lost ! Please try again", getActivity());
} else if (error instanceof NetworkError) {
CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No internet connection", getActivity());
}
}
});

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(jsonObjectRequest);

} catch (JSONException e) {
e.printStackTrace();
}
}

最佳答案

您可以为您的请求设置标签。这将是每个请求的标识。之后,您的请求队列有一个 cancelAll 方法,它需要一个标签。

//在将请求添加到队列之前,设置标签,标签可以是字符串,或者类似的东西。

yourRequest.setTag(tag);

之后,当您想取消您的请求时:
requestQueue.cancelAll(tag);

关于android - 如何在android中取消单 Volley 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com