Android Volley JsonArrayRequest working half the time

252 Views Asked by At

I'm attempting to call my web apps API which successfully gets me a list of all "profiles" and displays them in a correct fashion (the JsonArrayRequest for profiles). However, I'm having issues where I'm calling a consecutive url which holds the "matches" for profiles indicated by a profile ID. When I call this APi and attempt to display the match amount below each profile, it works probably half the time. I think my problem might be in reusing the requestQueue but I have tried creating new ones and my problem still remains! Any help would be appreciated.

    import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 04-06-2015.
 */
public class GetSellerProfileFragment extends Fragment {
    private CustomListAdapter listAdapter;
    private static final String profileUrl = "http://172.16.98.152:3000/apip/sellers/profiles";
    private static final String matchCountUrl = "http://172.16.98.152:3000/apip/sellers/profiles/matches/counts";
    private ProgressDialog pDialog;
    private ListView listView;
    private List<BuyerProfile> buyersProfiles = new ArrayList<BuyerProfile>();
    private View root;
    private int matches;
    private String matchId;
    private FloatingActionButton fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_get_seller_profiles, container, false);
        RecyclerView rv = (RecyclerView) root.findViewById(R.id.rv);
        rv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setItemAnimator(new DefaultItemAnimator());
        final CustomSellerProfileFragment recyclerAdapter = new CustomSellerProfileFragment(buyersProfiles);
        rv.setAdapter(recyclerAdapter);
        rv.setHasFixedSize(true);
        RequestQueue mRequestQueue;

        Cache cache = new DiskBasedCache(getActivity().getCacheDir(), 1024 * 1024);
        Cache backupCache = new DiskBasedCache(getActivity().getCacheDir(), 1024* 1024);

        Network network = new BasicNetwork(new HurlStack());
        mRequestQueue = new RequestQueue(cache, network);
        mRequestQueue.start();

        JsonArrayRequest profileRequest = new JsonArrayRequest(profileUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                BuyerProfile parsedProfile = new BuyerProfile();
                                parsedProfile.setBuyerProfTitle(obj.getString("title"));
                                parsedProfile.setDescription(obj.getString("description"));
                                parsedProfile.setLocations(obj.getString("locations"));
                                parsedProfile.setAssetTypes(obj.getString("asset_type"));
                                parsedProfile.setPropertyStatuses(obj.getString("property_status"));
                                parsedProfile.setProfileId(obj.getString("id"));
                                //parsedProfile.setMatchCount(matches);
                                //parsedProfile.setBuyerId("Select");
                                buyersProfiles.add(parsedProfile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        recyclerAdapter.notifyDataSetChanged();
                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        //hidePDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Toast.makeText(selectBuyerProfile.this,"Error",Toast.LENGTH_LONG).show();

            }
        });

        JsonArrayRequest matchRequest = new JsonArrayRequest(matchCountUrl,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                for(int k = 0; i < buyersProfiles.size(); k++) {
                                    if(buyersProfiles.get(k).getProfileId() == obj.getString("id")) {
                                        buyersProfiles.get(k).setMatchCount(obj.getString("count"));
                                        System.out.println(obj.getString("count"));
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Toast.makeText(selectBuyerProfile.this,"Error",Toast.LENGTH_LONG).show();
                System.out.println(error);

            }
        });

        mRequestQueue.add(matchRequest);
        mRequestQueue.add(profileRequest);
        return root;
    }
}

And here is an example of a response to the Matchrequest API.

[{"id":1,"count":"1"}]

And here is a response from the sellers profile API

{"id":1,"title":"Test Seller","profile_status":"ACTIVE","description":"Testing Seller Profile","locations":"Mid-Wilshire","asset_type":"RETAIL","property_status":"COMING_SOON","role":"PRINCIPAL","investment_lower":"650000","investment_upper":"900000","seller_id":2,"created_on":"2016-06-14T18:09:27.411Z","updated_on":"2016-06-14T18:09:27.411Z","matching_opportunities":"1"}
1

There are 1 best solutions below

1
On

I figured out a workaround but not a direct answer to my question. Instead of bothering to call the MATCH api I realized I could just pull directly from the profile api and use the "matching_opportunities" part of the JSON Array.