in my cache mechanism after add the request to the RequestQueue and try to fetch data from server, my cache method return multi duplicate result from server return json string
RequestQueue queue = Volley.newRequestQueue(this);
String url = Globals.getHostAddress() + "/get_latest_video";
items.clear();
CacheRequest cacheRequest = new CacheRequest(0, url, new Response.Listener<NetworkResponse>() {
#Override
public void onResponse(NetworkResponse response) {
try {
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
Log.e("allVideos", jsonString);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
queue.add(cacheRequest);
server return this result:
{"id":12,"channel_id":7,"category_id":8}
Cache mechanism return :
{"id":12,"channel_id":7,"category_id":8},{"id":12,"channel_id":7,"category_id":8}
whats my code problem to cache data and using that?
CacheRequest class content:
public class CacheRequest extends Request<NetworkResponse> {
private final Response.Listener<NetworkResponse> mListener;
private final Response.ErrorListener mErrorListener;
public CacheRequest(int method, String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
this.mErrorListener = errorListener;
}
#Override
protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
return Response.success(response, cacheEntry);
}
#Override
protected void deliverResponse(NetworkResponse response) {
mListener.onResponse(response);
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
#Override
public void deliverError(VolleyError error) {
mErrorListener.onErrorResponse(error);
}
}
UPDATED POST:
public class ActivityBootstrap extends Activity implements AdapterView.OnItemClickListener {
#InjectView(R.id.drawer_list)
ListView drawer_list;
#InjectView(R.id.drawer_layout)
DrawerLayout drawer_layout;
#InjectView(R.id.listView)
AsymmetricGridView listView;
private List<DrawerItem> drawer_items = new ArrayList<DrawerItem>();
private List<VideoItems> items = new ArrayList<>();
private DrawerAdatper drawer_adatper = null;
private boolean mDrawerState = false;
private VideoAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
RequestQueue queue = Volley.newRequestQueue(this);
String url = Globals.getHostAddress() + "/get_latest_video";
items.clear();
CacheRequest cacheRequest = new CacheRequest(0, url, new Response.Listener<NetworkResponse>() {
#Override
public void onResponse(NetworkResponse response) {
try {
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
Log.e("allVideos", jsonString);
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject video_single_item = jsonArray.getJSONObject(i);
int video_id = video_single_item.getInt("id");
String video_thumbnail = video_single_item.getString("thumbnail");
int colSpan = Math.random() < 0.2f ? 2 : 1;
int rowSpan = colSpan;
VideoItems item = new VideoItems(colSpan, rowSpan, video_thumbnail,video_id);
items.add(item);
}
setUpListView(items);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
queue.add(cacheRequest);
}
private void setUpListView(List video_array_items) {
final List<AsymmetricItem> items = new ArrayList<>();
adapter = new VideoListAdapter(this, video_array_items);
AsymmetricGridViewAdapter asymmetricAdapter =
new AsymmetricGridViewAdapter<>(this, listView, adapter);
listView.setAdapter(asymmetricAdapter);
listView.setRequestedColumnCount(3);
listView.setAdapter(getNewAdapter());
listView.setOnItemClickListener(this);
listView.setAllowReordering(true);
listView.isAllowReordering();
}
private AsymmetricGridViewAdapter<?> getNewAdapter() {
return new AsymmetricGridViewAdapter<>(this, listView, adapter);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
int video_position = items.get(position).getVideo_id();
Intent intent = new Intent(ActivityBootstrap.this, ActivitySHowVideoDetaile.class);
intent.putExtra("video_id", video_position);
startActivity(intent);
}
}
UPDATED POST 2)
Result of Log.e("allVideos", jsonString); is :
10-28 09:51:46.016 17872-17872/pishguy.ir.asrebidree E/allVideos: [{"id":12,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"},{"id":10,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"}]
10-28 09:51:47.249 17872-17872/pishguy.ir.asrebidree E/allVideos: [{"id":12,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"},{"id":10,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"}]
Result of server response i'm test it with browser:
[{"id":12,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"},{"id":10,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"}]
UNFORTUNATELY this duplicate result is random(NOT duplicate every time)
UPDATE 3:
10-28 13:14:52.322 16129-16129/pishguy.ir.asrebidree E/Test: onCreate called
10-28 13:14:52.513 16129-16129/pishguy.ir.asrebidree E/allVideos: [{"id":12,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"},{"id":10,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"}]
10-28 13:14:53.206 16129-16129/pishguy.ir.asrebidree E/allVideos: [{"id":12,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"},{"id":10,"thumbnail":"1445615983c92d10f1f3ee907150be1fdb8184ca893178682-360p__19106.png"}]
Because I have commented too many lines and I cannot re-produce "duplication behavior" as your current issue, so I post my answer here, hope this helps!
Inside CacheDispatcher class, you will find the following:
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
#Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}
IMO, it is possible the softTtl that is causing this duplication behavior.
I suggest that you use the following line to setRetryPolicy for your request:
cacheRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the RequestQueue.
queue.add(cacheRequest);
Related
I apply cache but it works only one time when I click on first item it shows data after that when I click on another it shows same data how t fix it any guidelines
Here is my code
String URL = "http://facekart.azanic.com/Data_show_all_.php";
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Fetcing, please wait...");
progressDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("result");//getting array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
Itemssetget it = new Itemssetget();
it.setName(jsonObject1.getString("name"));
it.setPhonenumber_seller(jsonObject1.getString("phonenumber_seller"));
it.setPrice(jsonObject1.getString("price"));
it.setDiscountprice(jsonObject1.getString("discountprice"));
it.setUnits(jsonObject1.getString("units"));
it.setStock(jsonObject1.getString("stock"));
it.setId(jsonObject1.getString("key_auto"));
it.setImageurl("http://facekart.azanic.com/images/" + jsonObject1.getString("imageurl"));
if (it.getStock().toString().equals("In stock")) {
items_random.add(it);
}
}
if (!items_random.isEmpty() && getActivity() != null) {
myAdapter = new homebuyer_fruits_adapter(getActivity(), items_random, homebuyer.phone_number_shop);
myAdapter.notifyDataSetChanged();
Random_list.addHeaderView(random_v);
Random_list.setAdapter(myAdapter);
Random_list.setSmoothScrollbarEnabled(true);
loadingdataprogress.stopShimmerAnimation();
loadingdataprogress.setVisibility(View.INVISIBLE);
// Toast.makeText(getContext(),"ADDED",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getContext(), "Some error occurred -> " + volleyError, Toast.LENGTH_LONG).show();
;
}
}) {
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new String(jsonString), cacheEntry);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
#Override
protected void deliverResponse(String s) {
super.deliverResponse(s);
}
#Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
//adding parameters to send
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("s_number", homebuyer.phone_number_shop);
parameters.put("trig", "all");
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(getContext());
rQueue.add(request);
Thanks in advance if any one help me in this I want every item click it shows data but first time it get from service second time it use from cache r any other method you can also suggest me How I smooth my app
I don't want to loading again and again
I am using Volley cache on my recycler view data. It works fine in online. When i switch to offline it displays all the data correctly but whenever i Switch back from offline to online, It creates duplicate copies on first launch while disappears on restarting the app.
Here is Main Activity code :
private JsonArrayRequest getDataFromServer(int requestCount) {
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar2);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
Intent intent = getIntent();
String gradevalue = intent.getExtras().getString("gradename");
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.CATEGORY_URL + String.valueOf(requestCount) + Config.CATEGORY_URL2+ gradevalue ,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
//Creating the superhero object
Categories gradee = new Categories();
JSONObject json = null;
try {
//Getting json
json = response.getJSONObject(i);
//Adding data to the superhero object
gradee.setImageUrl(json.getString(Config.TAG_C_IMAGE_URL));
gradee.setName(json.getString(Config.TAG_C_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
listCategories.add(gradee);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
}
}) {
#Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60 * 1000;
final long cacheExpired = 24 * 60 * 60 * 1000;
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString), cacheEntry);
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
#Override
protected void deliverResponse(JSONArray response) {
super.deliverResponse(response);
}
#Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
return jsonArrayRequest;
}
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
requestCount++;
} private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
}
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Ifscrolled at last then
if (isLastItemDisplaying(recyclerView1)) {
//Calling the method getdata again
getData();
}
}
When Switched from Offline to Online, It gives duplicate views which disappers on restart, Heres the screenshot
:
If anyone can look up into code and figure out the problem, I am sure it is a minor problem. Let me know the possible ways to fix this up. Thanks.
Not able to comment. You can put the JSON reponse into a list and clear it each time you're offline. Or put the response into a LinkedHashSet, clear the list and then populate that list with the set.
listCategories.add(gradee));
LinkedHashSet<Categories> set = new LinkedHashSet<>(listCategories);
listCategories.clear();
listCategories.addAll(new ArrayList<>(set));
This should remove the duplicates.
I have json data from webservice that should be stored in two lists, String list and Integer List. the one for strings stores the data fine but the one for the integers doesn't. it prompts error saying java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
here is the complete code
public class Setup extends AppCompatActivity {
Button save;
EditText schoolPK;
String schoolname, classname, cityname, school_id,class_id;
ImageView logout;
GridView grid;
String cityid,classid;
int schoolid;
ArrayList<String> CityName, CityId;
private List<Integer> classID=new ArrayList<Integer>();
private List<String> listschools = new ArrayList<String>();
private List<String> listclasses = new ArrayList<String>();
Spinner schoolspinner;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String classnames = "classname";
public static final String schoolnames = "schoolname";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
String saved_letter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup2);
CityName=new ArrayList<>();
CityId = new ArrayList<>();
//classID = new ArrayList<>();
// schoolID = new ArrayList<>();
schoolPK = (EditText) findViewById(R.id.schoolPK);
final Spinner city = (Spinner) findViewById(R.id.city);
final Spinner classspinner = (Spinner) findViewById(R.id.classname);
String URL_countries="http://192.168.0.148/Election/api/school";
JsonArrayRequest items = new JsonArrayRequest(URL_countries, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
for (int i = 0;i<response.length();i++){
try {
JSONObject obj = response.getJSONObject(i);
String country=obj.getString("name_Ar");
String cityid = obj.getString("pK_ID");
CityId.add(cityid);
CityName.add(country);
}
catch (JSONException e) {
e.printStackTrace();
}
city.setAdapter(new ArrayAdapter<String>(Setup.this, android.R.layout.select_dialog_item, CityName));
}
//to get me the
}
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
AlertDialog.Builder add = new AlertDialog.Builder(Setup.this);
add.setMessage(error.getMessage()).setCancelable(true);
AlertDialog alert = add.create();
alert.setTitle("error");
alert.show();
}
} );
Controller.getInstance(Setup.this).addToRequestque(items);
city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cityname = parent.getItemAtPosition(position).toString();
cityid = CityId.get(position).toString();
//CityId.clear();
listclasses.clear();
getclasses();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent) {
}
});
classspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
classname = parent.getItemAtPosition(position).toString();
class_id = classID.get(position).toString();
}
// to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent) {
}
});
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedpreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
Intent intent = new Intent(getApplicationContext(), Vote.class);
editor.putString("schoolname", cityname);
editor.putString("schoolid", cityid);
editor.putString("classname", classname);
editor.putString("classid", class_id);
editor.apply();
}
});
}
public void getschools(){
try {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String URL = "http://192.168.0.148/Election/api/School";
JSONObject jsonBody = new JSONObject();
// jsonBody.put("tblRegisteredUsers_nickName", username.getText().toString().trim());
jsonBody.put("cityFK_ID", cityid);
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//my response later should be changed
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
})
{
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString;
String json = null;
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
responseString = String.valueOf(json).trim();
ArrayList<Schoolresponse> list_response = new ArrayList<Schoolresponse>();
Type listType = new TypeToken<List<Schoolresponse>>() {}.getType();
list_response = new Gson().fromJson(responseString, listType);
// schoolID= new ArrayList<>();
for (int i = 0; i < list_response.size(); i++) {
School listItemData = new School();
listItemData.setName(list_response.get(i).getNameAr());
listItemData.setId(list_response.get(i).getPKID());
//schoolID.add(listItemData.getId());
listschools.add(listItemData.getName());
}
// i should have this piece of code for methods that are running in the background
runOnUiThread(new Runnable() {
#Override
public void run() {
schoolspinner.setAdapter(new ArrayAdapter<String>(Setup.this, android.R.layout.select_dialog_item, listschools));
}
});
return Response.success(list_response.toString(), HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
public void getclasses(){
final Spinner classspinner = (Spinner) findViewById(R.id.classname);
try {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String URL = "http://192.168.0.148/Election/api/classes";
JSONObject jsonBody = new JSONObject();
// jsonBody.put("tblRegisteredUsers_nickName", username.getText().toString().trim());
jsonBody.put("SchoolFK_ID", cityid);
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//my response later should be changed
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
})
{
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString;
String json = null;
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
responseString = String.valueOf(json).trim();
ArrayList<ClassResponse> list_response = new ArrayList<ClassResponse>();
Type listType = new TypeToken<List<ClassResponse>>() {}.getType();
list_response = new Gson().fromJson(responseString, listType);
for (int i = 0; i < list_response.size(); i++) {
School listItemData = new School();
listItemData.setName(list_response.get(i).getNameAr());
listItemData.setId(list_response.get(i).getPKID());
classID.add(listItemData.getId());
listclasses.add(listItemData.getName());
}
// i should have this piece of code for methods that are running in the background
runOnUiThread(new Runnable() {
#Override
public void run() {
classspinner.setAdapter(new ArrayAdapter<String>(Setup.this, android.R.layout.select_dialog_item, listclasses));
}
});
// String Check = yourModel.getMessagetitle();
return Response.success(list_response.toString(), HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
and here is the area I'm talking about
where only the string list (listclasses) is storing data while the integer (classID) list does not
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString;
String json = null;
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
responseString = String.valueOf(json).trim();
ArrayList<ClassResponse> list_response = new ArrayList<ClassResponse>();
Type listType = new TypeToken<List<ClassResponse>>() {}.getType();
list_response = new Gson().fromJson(responseString, listType);
for (int i = 0; i < list_response.size(); i++) {
School listItemData = new School();
listItemData.setName(list_response.get(i).getNameAr());
listItemData.setId(list_response.get(i).getPKID());
classID.add(listItemData.getId());
listclasses.add(listItemData.getName());
}
// i should have this piece of code for methods that are running in the background
runOnUiThread(new Runnable() {
#Override
public void run() {
classspinner.setAdapter(new ArrayAdapter<String>(Setup.this, android.R.layout.select_dialog_item, listclasses));
}
});
// String Check = yourModel.getMessagetitle();
return Response.success(list_response.toString(), HttpHeaderParser.parseCacheHeaders(response));
}
};
hope I get right answer
thanks in advance
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
means that your Array is empty and you are trying to access array element from an empty array.
You can put a condition to check if array length is more than 0 and then proceed using it like:
if(array != null && array.length > 0){
// do parsing
}
Make sure your class School contains variable with name pK_ID. Look carefully on the upper/lower cases in the variable name.
If the variable name is not matching with JSON tag then this will result in empty fetching from JSON, even if you are getting valid JSON from URL as per your comment.
can anyone please help me i can not figure out the problem in the code. i only want to load the json response once when application startup or when activity reload but it loading json response twice in recylerview everytime when application start and making two copies of every json object retrive from server in my case there total 50 news channel in recylerview instead of 25 news channel
public class General extends Fragment
{
private static final String
URL = "https://newsapi.org/v1/sources?language=en&category=general&apiKey=0e1b2f7bc6bd4e1fbe0b40bea257dc97";
private final int android_image_urls[] =
{
R.drawable.abcnews,
R.drawable.aljazeera,
R.drawable.associatedpress,
R.drawable.bbcnews,
R.drawable.cnn,
R.drawable.googlenews,
R.drawable.independent,
R.drawable.metro,
R.drawable.mirror,
R.drawable.newsweek,
R.drawable.newyorkmagazine,
R.drawable.reddit,
R.drawable.reuters,
R.drawable.theguardianau,
R.drawable.theguardianuk,
R.drawable.thehindu,
R.drawable.thehuffingtonpost,
R.drawable.thenewyorktimes,
R.drawable.thetelegraph,
R.drawable.thetimesofindia,
R.drawable.thewashingtonpost,
R.drawable.time,
R.drawable.usatoday
};
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private ArrayList<AndroidVersion> androidVersions;
TextView textView;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_general, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),3);
recyclerView.setLayoutManager(layoutManager);
textView = (TextView) rootView.findViewById(R.id.empty_view);
androidVersions = new ArrayList<>();
loadRecyclerViewData();
return rootView;
}
public void loadRecyclerViewData() {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading Data....");
progressDialog.show();
JsonObjectRequest data = new JsonObjectRequest(Request.Method.GET, URL,null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(String.valueOf(response));
JSONArray array = jsonObject.getJSONArray("sources");
for (int i = 0; i < array.length(); i++) {
JSONObject o = array.getJSONObject(i);
AndroidVersion item = new AndroidVersion(
o.getString("name"),
android_image_urls[i],
o.getString("category")
);
androidVersions.add(item);
}
if (androidVersions.isEmpty()) {
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
textView.setVisibility(View.GONE);
}
adapter = new DataAdapter(androidVersions, getContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getContext(),"No Internet Connection", Toast.LENGTH_SHORT).show();
}
})
{
#Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheHitButRefreshed = 3 * 60*1000; // in 3 minutes cache will be hit, but also refreshed on background
final long cacheExpired = 24 * 60 * 1000 ; //24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null)
{
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString), cacheEntry);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
#Override
protected void deliverResponse(JSONObject response) {
super.deliverResponse(response);
}
#Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(data);
}
}
Your code is correct , it must not show the duplicate data , try removing the caching of data
In you onResponse you are not cleaning the androidVersions list. So each time your request is executed you add new items to your list. Most probably your request is executed twice (loadRecyclerViewData() is called twice?).
Try to clean or recreate androidVersions every time you fill it in onResponse.
I want develop android application for one website. I read website posts from json and show its in RecyclerView every 10 posts.
I can show title, description and thumbnail. but i want show medium from thumbnail_images instance of thumbnail. I don't know how to read images from medium ?!
My Json Link : Link
AsyncTaskCodes:
public class MainDataInfo {
private Context mContext;
private String ServerAddress = ServerIP.getIP();
public void getMainDataInfo(Context context) {
mContext = context;
new getInfo().execute(ServerAddress + "page=1");
}
private class getInfo extends AsyncTask<String, Void, String> {
EventBus bus = EventBus.getDefault();
private String ou_response;
private List<MainDataModel> infoModels;
#Override
protected void onPreExecute() {
CustomProcessDialog.createAndShow(mContext);
infoModels = new ArrayList<>();
}
#Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(ServerAddress + "page=1")
.build();
Response response;
try {
response = client.newCall(request).execute();
ou_response = response.body().string();
response.body().close();
if (ou_response != null) {
try {
JSONObject postObj = new JSONObject(ou_response);
JSONArray postsArray = postObj.getJSONArray("posts");
infoModels = new ArrayList<>();
for (int i = 0; i <= infoModels.size(); i++) {
JSONObject postObject = (JSONObject) postsArray.get(i);
int id = postObject.getInt("id");
String title = postObject.getString("title");
//get other data
JSONObject imageObj = postObject.getJSONObject("thumbnail_images");
JSONObject mediumObj = imageObj.optJSONObject("medium");
String mediumImage = mediumObj.getString("url");
Log.d("Data", "Post id: " + id);
Log.d("Data", "Post title: " + title);
//Use the title and id as per your requirement
infoModels.add(new MainDataModel(
postObject.getInt("id"),
postObject.getString("title"),
postObject.getString("content"),
postObject.getString(mediumImage)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return ou_response;
}
#Override
protected void onPostExecute(String result) {
CustomProcessDialog.dissmis();
if (result != null) {
bus.post(infoModels);
}
}
}
}
for fetch medium image i use this code :
//get other data
JSONObject imageObj = postObject.getJSONObject("thumbnail_images");
JSONObject mediumObj = imageObj.optJSONObject("medium");
String mediumImage = mediumObj.getString("url");
but when set mediumImage for infoModels.add(new MainDataModel() not show me any posts!
How can set images from medium ? thanks all <3
private void setImageWithPicaso(String imageUrl) {
if (!(imageUrl == null)) {
Picasso.with(getActivity()).load(imageUrl).placeholder(R.drawable.placeholder_background).into(imageView, new Callback() {
#Override
public void onSuccess() {
//On Success
}
#Override
public void onError() {
spinner.setVisibility(View.GONE);
//On Error
}
});
} else {
spinner.setVisibility(View.GONE);
//On Error
}
}