Volley json parsing displays nothing in ListView - android

I would like to display movie titles from themoviedb.org. But the applications is not displaying anything in the ListView. Here is my code:
try{
JsonObjectRequest request = new JSonObjectRequest(Request.Method.Get,url,null,new Response.Listener<JSONObject>(){
#Override
public void onResponse(JSONObject response){
try{
JSONArray titlesArray =response.getJSONArray("results");
for(int i=0;i<titlesArray.length();i++){`
JSONObject inner = titlesArray.getJSONObject(i);
String id=inner.getString("id");
String title =inner.getString("title");
String c= id+title;
arrayListMovies=new ArrayListMovies<>();
arrayListMovies.add(c);
}
RequestQueue requestQueue=Volley.newRequestQueue(this);
requestQueue.add(request);
And I am setting the arrayadapter to movieList ListView outside of the Volley function
moviesList = findViewById(R.id.listViewForMovies);
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),android.R.layout.android_simple_list_item1,arrayListMovies);
moviesList.setAdapter(adapter);
Thanks.

I would not recommend make REST API calls the way you are doing, this is prone to errors, I recommend you to check about retrofit:
https://square.github.io/retrofit/
You have various problems with your code:
You are creating the adapter and assigning data before the call to the API completes.
You are creating the string list on every iteration of your titlesArray
You are not notifying the adapter that the list have changed.
Dont use a try block inside your response, because if there is errors with your code, it will be useful that the app crashes in order to get notified about this errors.
public class Test extends AppCompatActivity {
private ArrayAdapter<String> myAdapter;
private ArrayList<String> arrayListMovies = new ArrayList<>();
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
this.myAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.android_simple_list_item1, arrayListMovies);
this.makeRequest();
}
public void makeRequest() {
try {
JsonObjectRequest request = new JSonObjectRequest(Request.Method.Get, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
JSONArray titlesArray = response.getJSONArray("results");
arrayListMovies = new ArrayList<>();
for (int i = 0; i < titlesArray.length(); i++) {
JSONObject inner = titlesArray.getJSONObject(i);
String id = inner.getString("id");
String title = inner.getString("title");
String c = id + title;
arrayListMovies.add(c);
}
myAdapter.notifyDataSetChanged();
RequestQueue requestQueue = Volley.newRequestQueue(this);
}
}
}
}
}
Note some things: I have moved the adapter and arrayListMovies declarations to the top of the class.
I initialize the adapter before making the api call.
In the response of the network call I create the arrayListMovies ONCE.
At the end i call myAdapter.notifyDataSetChanged(); to refresh the adapter data.

Related

Why does this ArrayList not retain it's value unless it is static? [duplicate]

This question already has answers here:
ArrayList being empty after adding elements
(2 answers)
Using volley library, inside the onResponse Method the ArrayList have some data but outside the OnResponse method the arraylist is empty
(1 answer)
Closed 5 years ago.
So, I've got the following code that hits up a database for some info:
// This is the arraylist I am talking about
private ArrayList<String> results = new ArrayList<String>();
private String getAllSongsScriptUrl =
"https://songs-list-site-test.herokuapp.com/get_songs_list.php";
private RequestQueue requestQueue;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_songs);
requestQueue = Volley.newRequestQueue(getApplicationContext());
// Prepare the list to display the results
ArrayList<String> results = getAllSongsFromDatabase();
resultsListAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, results
);
Log.d("fuck", results.toString());
resultsList = (ListView)findViewById(R.id.songs_list_view);
resultsList.setAdapter(resultsListAdapter);
}
private ArrayList<String> getAllSongsFromDatabase()
{
Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, getAllSongsScriptUrl,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response)
{
try
{
JSONArray songs = response.getJSONArray("songs");
for(int i = 0; i < songs.length(); i++)
{
// Get the data from the JSON response
JSONObject song = songs.getJSONObject(i);
String artist = song.getString("artist");
String song_name = song.getString("name");
// Construct a result string and add it to listview
String result =
artist.toString() +
" - " +
song_name.toString();
results.add(result);
// POINT A: Printing results works fine here
}
}
catch(JSONException e)
{
// TODO: Handle
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error)
{
// TODO: Handle
}
}
);
requestQueue.add(jsonObjectRequest);
// POINT B, where things start to go pear shaped
return results;
}
If I print the value of results at comment marked POINT A, I get a filled arraylist as expected. However, if I print the results at POINT B, I get an empty arraylist.
Not being one to give up easily, I fiddled with the code, trying to get it to work. I noticed, interestingly, that if I marked results as static, everything seems to work. WHY?

how to clear RecyclerView adapter data

Here in my UI, i have used two buttons to load different data to a RecyclerView. First time the data is displaying properly on click of each button. But if i click the button for the second time the data is adding to the adapter twice. I mean the the adapter is not cleared. it is keep on adding the data on click of button. I Think i have to do something with the adapter on click of a button. Can anyone pls let me know how to clear the adapter or where i am going wrong..
Here is the code.
public class GstVendorLocRetrieve extends AppCompatActivity {
private String vault;
private TextView txt;
public static final String DATA_URL = "http://oursite.com/getgstvendorlocation.php?vault_no=";
public static final String DATA_URL1 = "http://oursite.com/getgstcustomerlocation.php?vault_no=";
//Tags for my JSONRes
public static final String TAG_VendorID = "VendorID";
public static final String TAG_CustomerID = "Customer_ID";
public static final String TAG_ADDRESS = "Address";
private Button vendor;
private Button customer;
//Creating a List of superheroes
private List<GstVendLoc> listSuperHeroes;
private List<GstCustLoc> listSuperHeroes1;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationretrieve);
SharedPreferences sharedPreferences = getSharedPreferences(GstLogin.SHARED_PREF_NAME, MODE_PRIVATE);
vault = sharedPreferences.getString(GstLogin.EMAIL_SHARED_PREF,"Not Available");
vendor = (Button) findViewById(R.id.login);
customer = (Button) findViewById(R.id.login1);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
listSuperHeroes1 = new ArrayList<>();
vendor.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recyclerView.setAdapter(null);
getData();
}
});
customer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recyclerView.setAdapter(null);
getData1();
}
});
}
//This method will get data from the web api
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL+vault,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GstVendLoc gst1 = new GstVendLoc();
JSONObject json = null;
try {
json = array.getJSONObject(i);
gst1.setVendorID(json.getString(TAG_VendorID));
gst1.setAddress(json.getString(TAG_ADDRESS));
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(gst1);
}
//Finally initializing our adapter
adapter = new CardAdapter17(listSuperHeroes, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
private void getData1(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL1+vault,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData1(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData1(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GstCustLoc gst1 = new GstCustLoc();
JSONObject json = null;
try {
json = array.getJSONObject(i);
gst1.setCustomer_ID(json.getString(TAG_CustomerID));
gst1.setAddress(json.getString(TAG_ADDRESS));
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes1.add(gst1);
}
//Finally initializing our adapter
adapter = new CardAdapter18(listSuperHeroes1, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
}
Use this code for clear RecycleView items
public void clear() {
int size = listSuperHeroes.size();
listSuperHeroes.clear();
notifyItemRangeRemoved(0, size);
}
You need to clear your Array List before you get data second time.
Do this inside parseData1 method before for loop.
listSuperHeroes.clear();
listSuperHeroes1.clear();
What you have to do is Update RecyclerView on button Click , Put below method in your adapter
public void updateData(ArrayList<ViewModel> viewModels) {
items.clear();
items.addAll(viewModels);
notifyDataSetChanged();
}
Than call this method with new data
ArrayList<ViewModel> viewModelsWithNewData = new ArrayList<ViewModel>();
adapter.updateData(viewModelsWithNewData );
you dont need to set adapter after geting data from the online
//Finally initializing our adapter
adapter = new CardAdapter18(listSuperHeroes1, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
you can initialize of set the adapter in the on create and add data in the 'listSuperHeroes1' and after parse data you can do adapter.notifyDataSetChanged();
this will change the list data.
and the solution for the getting the previous data you have to remove the all data from the listsuperHeroes1 this will help you if you getting any problem please comment .
I am just improving #Rony's answer.
If you should always check if the ArrayList is not null before attempting to call .size(), otherwise, you might end up with a null pointer exception
if (listSuperHeroes != null && !listSuperHeroes.isEmpty()) {
int size = listSuperHeroes.size();
listSuperHeroes.clear();
notifyItemRangeRemoved(0, size);
}

Android: not able to add listview items dynamically through volley

Hi I'm working on CardSwipe functionality like tinder. (For understanding see the attached image)
For this i followed this code, when using static data like bellow, I'm successfully adding items in ListView
private void loadCards2(){
itemsaArrayList = new ArrayList<>();
for(int i = 0; i < 10; i++){
CardSwipeItems items = new CardSwipeItems();
items.setCardImageUrl("http://i.ytimg.com/vi/PnxsTxV8y3g/maxresdefault.jpg");
items.setCardDescription("But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness.");
itemsaArrayList.add(items);
}
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
flingContainer.setAdapter(myAppAdapter);
initControlls();
}
But when I'm trying to add the items dynamically by using volley means, items are not adding in the ListView. (For this volley
please see the loadCards() method in the CardSwipeActivity)
I tried lot of approaches to load the items in the list view dynamically. For ex: I used Thread (For code see this) and i also used Handler (For code see this) but I'm not able to add the items in ListView dynamically
If any one know the solution for this means please tell me. Thank you.......
Edit
*My method*
private void loadCards(){
String Url = "myApi";
Log.e("Url", Url);
final ProgressDialog dialog = ProgressDialog.show(CardSwipeActivity.this, null, null);
ProgressBar spinner = new android.widget.ProgressBar(CardSwipeActivity.this, null,android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(spinner);
dialog.setCancelable(false);
dialog.show();
StringRequest request = new StringRequest(Request.Method.GET, Url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
dialog.dismiss();
if(response != null && !response.startsWith("<HTML>")){
Log.e("OnResponse", response);
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray.length() > 0){
itemsaArrayList = new ArrayList<>();
for(int i = 0; i< jsonArray.length(); i++){
JSONObject singleObj = jsonArray.getJSONObject(i);
String imgId = singleObj.getString("avatar_file_id");
String name = singleObj.getString("name");
CardSwipeItems items = new CardSwipeItems();
Log.e("imgId", imgId);
Log.e("name", name);
items.setCardImageUrl(imgId);
items.setCardDescription(name);
itemsaArrayList.add(items);
}
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
flingContainer.setAdapter(myAppAdapter);
initControlls();
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
Log.e("Internet", "Internet");
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
if(error != null){
Log.e("error", error.toString());
}else{
}
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
params.put("user_id","386");
Log.e("Headers", "**********Headers*************");
Log.e("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
Log.e("user_id","386");
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(CardSwipeActivity.this);
queue.add(request);
queue.getCache().remove(Url);
}
You can add items dynamically to the adapter by addding items to the list you passed originally to adapter like this:
itemsaArrayList.add(items);
than you need to notify the adapter like this from your UI thread:
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
And to call it on the UI-Thread, use have to use runOnUiThread() of Activity. Then only, notifyDataSetChanged() will work.
Also have a look at this post
If you are getting response from server then try to move below line from onResponse method to onCreate method.
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
Hope this will work

JSON Download # onCreateView leaves recyclerView empty

if (isConnected()) {
Event eInstance = new Event();
theEvents = eInstance.downloadEvents(eventsNightlife, getActivity());
rAdapter = new RecyclerAdapter(theEvents);
recyclerView.setAdapter(rAdapter);
progrsBar.setVisibility(View.GONE);
....
This is part of the code that runs at "onCreateView". The method downloadEvents uses Volley to download JSON data, extract it and return a list of items (theEvents). Now when my app starts, the recycler view is empty. If I go to my home screen out of the app and then run my app again, this time the data sometimes gets downloaded.
I debugged step by step, and at first launch (i mean when the app is not just resuming), theEvents is empty, so the download didn't return or manage to return anything...
Suggestions on how to execute things before the UI has been shown to the user or what actually needs to be done to approach this task better?
Also, I use a swipeRefreshLayout and at its onRefresh method I do:
public void onRefresh() {
Event eInstance = new Event();
theEvents = eInstance.downloadEvents(eventsNightlife, getActivity());
rAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
but it doesn't work. I also tried to
rAdapter = new RecyclerAdapter(theEvents);
rAdapter.notifyDataSetChanged();
recyclerView.swapAdapter(rAdapter, false);
still not working.
EDIT: My downloadEvents method implementing Volley:
public List<Event> downloadEvents(String urlService, Context context) {
eventsList = new ArrayList<>();
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.GET, urlService, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
String durationStr = null;
for (int i = 0; i < response.length(); i++) {
JSONObject eventJson = response.getJSONObject(i);
String title = eventJson.getString("EventTitle");
String body = eventJson.getString("EventBody");
String date = eventJson.getString("EventDate");
String time = eventJson.getString("EventTime");
int duration = Integer.parseInt(eventJson.getString("EventDuration"));
if (duration > 60) {
durationStr = "Duration: " + duration / 60 + " h";
} else if (duration < 60) {
durationStr = "Duration: " + duration + " m";
}
String place = eventJson.getString("EventPlace");
String organ = eventJson.getString("Organization");
Event event = new Event(title, body, date, time, durationStr, place, organ);
eventsList.add(event);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY ERROR", "" + error);
}
}
);
requestQueue.add(jsonArrayRequest);
return eventsList;
}
You can use EventBus for your purpose that is a simple and truth way.
Here, i write an example for how to use EventBus with volley.
Consider that i want to download some data.
This is the class that my download methods is inside it (you can add more methods to it in the future):
Im used volley to download my data:
// Download methods is inside volley
public class MyDownloader{
public static void downloadData(){
DownloadDataEvent dlDataEvent=new DownloadDataEvent();
List<String> myResult=new ArrayList<>();
...
#Override
public void onResponse(JSONArray response) {
super.onResponse(response);
if(respone!=null){
// Do what i want with my received data
dlDataEvent.setData(response);
}
// Post my event by EventBus
EventBus.getDefault().post(dlDataEvent);
...
}
}
}
This is my event:
public class DownloadDataEvent{
private JSONArray mData;
public void setData(JSONArray data){
mData=data;
}
public JSONArray setData(){
return mData;
}
}
Now i want to use my downloadData() method inside my MainActivity:
(I called my downloadData method inside onCreate.)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// I have to register this class for EventBus subscriber:
if(!EventBus.getDefault().isRegister(this)){
EventBus.getDefault().registerSticky(this);
}
// Call my downloadData method
if(isConnected()){
MyDownloader.downloadData();
}
}
// And for receive the data through EventBus, i have to create a
// method (subscriber) in this template:
public void onEventMainThread(DownloadDataEvent downloadDataEvent){
JSONArray result=downloadDataEvent.getData();
// Do what i want with my received data
}
}
you can create more than one subscriber every where you want to use received data.
I passed JSONArray to my DownloadDataEvent that it is not good. you can deserialize your received data and pass it to your DownloadDataEvent.
I used Volley to download data
Maybe my descriptions were confusing, but EventBus is a well-known library and is very easy to use.

Which is the right method to make a connection with server using volley?

This is may be dumb question but i could not find the solution for my problem first of all am new for android development am making connection with server using google volley to bind the recyclerview in my fragment i have written the volley connection statement in oncreateview method whenever i open that fragment its making connection with server fetching the same record as it was before here what should i do is when response is made then only i need to fetch that data from server because of this am getting same data again and again whenever i open this fragment am really confused with this can anybody help me here let me post my code:
public class Task extends Fragment {
private static final String MY_PREFERENCE_KEY = "yogan";
private List<Model_Task_List> model_task_lists;
//Creating Views
Context context;
SharedPreferences.Editor editor;
private RecyclerView recyclerView;
Task_List_Adapter taskadapter;
private RecyclerView.LayoutManager layoutManager;
SharedPreferences sharedPreferences;
private RecyclerView.Adapter adapter;
Task_DB task_dbobj;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_task, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(layoutManager);
context=getActivity().getBaseContext();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Gson gsong = new Gson();
String jsons = sharedPreferences.getString(MY_PREFERENCE_KEY, "");
Type type = new TypeToken<ArrayList<Model_Task_List>>() {
}.getType();
model_task_lists = gsong.fromJson(jsons, type);
if(model_task_lists==null) {
model_task_lists = new ArrayList<Model_Task_List>();
//Showing a progress dialog
}
taskadapter = new Task_List_Adapter(model_task_lists, getContext());
recyclerView.setAdapter(taskadapter);
SharedPreferences sharedPreferences=getActivity().getSharedPreferences(LoginActivity.login,0);
String yogan=sharedPreferences.getString("user_id",null);
String Url = "http://xxx.xx.x.xxx/xxx/xxx.svc/getlist/GetTask/"+yogan;
//Creating a json array request
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, Url, new JSONObject(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
TaskList(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
Log.e("yog", error.toString());
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
return view;
}
public void TaskList(JSONObject jsonObject) {
String yogs = jsonObject.toString();
try {
JSONObject yog = new JSONObject(yogs);
JSONArray yogan = new JSONArray(yog.getString("GetTaskResult"));
for (int i = 0; i < yogan.length(); i++) {
Model_Task_List modelobj = new Model_Task_List();
JSONObject yogesh = yogan.getJSONObject(i);
modelobj.setSubject(yogesh.getString("Subject"));
modelobj.setUserName(yogesh.getString("UserName"));
modelobj.setTaskStatus(yogesh.getString("TaskStatus"));
model_task_lists.add(modelobj);
taskadapter.notifyDataSetChanged();
// task_dbobj.insert(modelobj);
}
//Finally initializing our adapter
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onStop() {
context=getActivity().getApplicationContext();
SharedPreferences sharedpref = PreferenceManager.getDefaultSharedPreferences(context);
editor = sharedpref.edit();
Gson gson=new Gson();
String god = gson.toJson(model_task_lists);
editor.putString(MY_PREFERENCE_KEY, god);
editor.commit();
super.onStop();
}
}
I would be very glad if someone tells the solution for my problem !!! you people are here to help beginners like us hope someone helps !!!
First of all, I would suggest to change your library, Volley is not completely upgraded to API 23 (since org.apache package is removed you will have to use legacy code), so I suggest to move to okHttp or some other library.
In case you want to stick with Volley you should create Volley.newRequestQueue(getContext()); inside onCreate() method in your Application class, and then reuse same queue, not creating new one each time inside fragment.

Categories

Resources