0

我正在尝试调用我的网络应用程序 API,它成功地为我获取了所有“配置文件”的列表并以正确的方式显示它们(配置文件的 JsonArrayRequest)。但是,我在调用一个连续的 url 时遇到问题,该 url 包含由配置文件 ID 指示的配置文件的“匹配项”。当我调用此 APi 并尝试在每个配置文件下方显示匹配量时,它可能工作了一半。我认为我的问题可能在于重用 requestQueue 但我已经尝试创建新的,但我的问题仍然存在!任何帮助,将不胜感激。

    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;
    }
}

以下是对 Matchrequest API 的响应示例。

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

这是来自卖家资料 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"}
4

1 回答 1

0

我想出了一个解决方法,但没有直接回答我的问题。我意识到我可以直接从配置文件 api 中提取并使用 JSON 数组的“matching_opportunities”部分,而不是费心调用 MATCH api。

于 2016-06-21T16:46:14.460 回答