2016-06-21 47 views
0

我正在嘗試調用我的web應用程序API,它成功地獲取了所有「配置文件」的列表,並以正確的方式顯示它們(用於配置文件的JsonArrayRequest)。但是,我遇到了問題,我正在調用連續的url,該URL包含配置文件ID指示的配置文件的「匹配」。當我打電話給這個APi並試圖在每個配置文件下面顯示匹配數量時,它可能工作一半時間。我認爲我的問題可能是重用requestQueue,但我嘗試了創建新的並且我的問題仍然存在!任何幫助,將不勝感激。Android Volley JsonArrayRequest工作時間的一半

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"} 
+0

如果matchRequest在profileRequest之前完成了怎麼辦?......如果不是?適配器哪裏知道MatchCount計數改變,因此它可以重新綁定視圖? – Selvin

+0

@Selvin這些變化仍然給我一個問題。我認爲它與重用緩存或請求隊列有關,但如果您在下面看到我的答案,我發現我可以直接從賣方配置文件API中獲取信息。 –

回答

0

我想出了一個解決方法,但沒有直接回答我的問題。與其不費力地調用MATCH api,我意識到我可以直接從配置文件API中取出並使用JSON數組的「matching_opportunities」部分。

+0

爲什麼theres api除了這個我不知道,但這解決了我的問題! –