我的凌空球在一段时间后多次呼叫,从服务器获取响应后再次呼叫多次。我已经尝试过使用排球默认重试策略并禁用缓存,但是它们都不起作用。
无论我在项目中使用排球还是在调用几次。
CustomStringRequestVolley
public class CustomStringRequestVolley {
private String url;
private String tag;
Context ctx;
private VolleyResponseListener volleyResponseListener;
public CustomStringRequestVolley(String url, String tag,Context ctx,VolleyResponseListener volleyResponseListener){
this.url = url;
this.tag = tag;
this.ctx=ctx;
this.volleyResponseListener = volleyResponseListener;
sendRequest();
}
private void sendRequest() {
final ProgressDialog pDialog = new ProgressDialog(ctx);
pDialog.setMessage("Loading ...");
pDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("catresponse", "response " + response);
pDialog.dismiss();
volleyResponseListener.onResponse(response, tag);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
stringRequest.setShouldCache(false);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
0, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getInstance(ctx).addToRequestQueue(stringRequest);
}
}
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private static Context mContext;
private MySingleton(Context context){
// Specify the application context
mContext = context;
// Get the request queue
mRequestQueue = getRequestQueue();
}
public static synchronized MySingleton getInstance(Context context){
// If Instance is null then initialize new Instance
if(mInstance == null){
mInstance = new MySingleton(context);
}
// Return MySingleton new Instance
return mInstance;
}
public RequestQueue getRequestQueue(){
// If RequestQueue is null the initialize new RequestQueue
if(mRequestQueue == null){
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
mRequestQueue.getCache().clear();
}
// Return RequestQueue
return mRequestQueue;
}
public<T> void addToRequestQueue(Request<T> request){
// Add the specified request to the request queue
getRequestQueue().add(request);
}
}
NavDrawerActivity.java
public class NavDrawerActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,VolleyResponseListener {
public static int notificationCountCart = 0;
EditText search_edittext;
TextView viewCart, orderNow, addToCart, txtItemName, txtItemDescriptions;
RecyclerView recyclerViewCategory, recyclerViewProduct;
ArrayList<CategoryModelClass> categoryArrayList;
ArrayList<SubCategoryModelClass> subCategoryModelClassList;
SubCategoryAdapter subCategoryAdapter;
CategoryAdapter categoryAdapter;
IntentFilter intentFilter;
MyReceiver receiver;
ShimmerFrameLayout shimmerContainer;
SharedPrefLogin sharedPrefLogin,sharedPrefLogout;
getCategoryInterface gtr;
RequestQueue queue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedPrefLogin = new SharedPrefLogin(getApplicationContext());
sharedPrefLogout = new SharedPrefLogin(getApplicationContext());
gtr =new getCategoryInterface() {
@Override
public void getSubCategory(String mainCategoryId) {
String mainCategoryID=mainCategoryId;
//Toast.makeText(getApplicationContext(),"Hello"+ mainCategoryId,Toast.LENGTH_SHORT).show();
fetchList(mainCategoryID);
}
};
categoryArrayList = new ArrayList<>();
subCategoryModelClassList = new ArrayList<>();
recyclerViewCategory = findViewById(R.id.recycler_view_horizontal);
recyclerViewProduct = findViewById(R.id.recycler_view_product_list);
search_edittext = findViewById(R.id.search_edittext);
/* viewCart=findViewById(R.id.txtv_viewCart);
orderNow=findViewById(R.id.txtv_OrderNow);
addToCart=findViewById(R.id.txtvAddToCart);*/
txtItemName = findViewById(R.id.txtItemName);
txtItemDescriptions = findViewById(R.id.txtItemDescriptions);
shimmerContainer = findViewById(R.id.shimmer_view_container);
shimmerContainer.startShimmerAnimation();
intentFilter = new IntentFilter();
intentFilter.addAction(Constants.CONNECTIVITY_ACTION);
categoryArrayList.add(new CategoryModelClass(0,"All Categories","https://logo.jpg"));
recyclerViewCategory.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
categoryAdapter = new CategoryAdapter(getApplicationContext(), categoryArrayList,gtr);
recyclerViewCategory.setAdapter(categoryAdapter);
recyclerViewProduct.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
subCategoryAdapter = new SubCategoryAdapter(getApplicationContext(), subCategoryModelClassList);
recyclerViewProduct.setAdapter(subCategoryAdapter);
SubCategoryInsertionAsyncTask myAsyncTasks = new SubCategoryInsertionAsyncTask();
myAsyncTasks.execute();
if (InternetUtils.checkForInternet(getApplicationContext())) {
categoryJSON();
//fetchJSON();
} else {
if (!InternetUtils.checkForInternet(getApplicationContext())) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("No Internet Connection");
alertDialogBuilder
.setMessage("Please check your internet connection. ")
.setCancelable(false).setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
receiver = new MyReceiver();
registerReceiver(receiver, intentFilter);
}
private void categoryJSON() {
CustomStringRequestVolley request1 = new CustomStringRequestVolley(URLs.categoryURL,"TAG1", this,this);
}
@Override
public void onResponse(String response, String tag) {
switch (tag) {
case "TAG1":
try {
Log.i("Responseeeeeezaq :", response.toString() + " " + tag);
// pDialog.hide();
JSONObject obj = new JSONObject(response);
JSONArray productArray = obj.getJSONArray("categories");
//now looping through all the elements of the json array
for (int i = 0; i < productArray.length(); i++) {
JSONObject productObject = productArray.getJSONObject(i);
CategoryModelClass categoryModelClass = new CategoryModelClass();
categoryModelClass.setCategoryID(productObject.getInt("Category-Id"));
categoryModelClass.setCategoryName(productObject.getString("Category-Name"));
categoryModelClass.setCategoryDesc(productObject.getString("Category-description"));
categoryModelClass.setCategoryImg(productObject.getString("Category-icon"));
categoryArrayList.add(categoryModelClass);
Log.d("zpuyi", String.valueOf(categoryArrayList));
}
categoryAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
Log.i("Responseeeeeezaq :", response.toString() + " " + tag);
}
@Override
public void onError(VolleyError error, String tag) {
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.CONNECTIVITY_ACTION)) {
if (InternetUtils.checkForInternet(context)) {
categoryJSON();
//fetchJSON();
SubCategoryInsertionAsyncTask myAsyncTasks = new SubCategoryInsertionAsyncTask();
myAsyncTasks.execute();
unregisterReceiver(receiver);
}
}
}
}
}