2017-11-11 71 views
0

我想同時解析兩個API,其中一個API獲得鍵的響應,該鍵具有API鏈接並通過另一個API StringRequest忘記響應。如何結合兩個請求,其中一個響應另一個API請求的響應

這是我的解析函數,首先解析一個api,並得到響應迴應href另一個api解析鏈接的關鍵用途名稱。 請儘快幫助我。 我搜索了,我得到了這個鏈接,但它不是正確的代碼。 https://www.versti.eu/TranslateProxy/https/stackoverflow.com/questions/37584001/how-to-combine-the-two-request-url-from-json-to-get-output-in-volley

private void parseSmartPhone() 
{ 
    StringRequest stringRequest= new        
    StringRequest(com.android.volley.Request.Method.GET, 
       Config.HOMECTEGORY, new Response.Listener<String>() { 

     @RequiresApi(api = Build.VERSION_CODES.N) 
     @Override 
     public void onResponse(String response) { 

      try { 
       //getting the whole json Array from the response 
       // JSONObject jsonObject= new JSONObject(response); 
       JSONArray jsonArray = new JSONArray(response); 
       mylist=new ArrayList<>(); 
       for (int i = 0;i<=2;i++) 
       { 
        latestsphone= new LatestSmartPhoneModel(); 

        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        JSONObject objtitle=jsonObject.getJSONObject("title"); 

        title=objtitle.getString("rendered"); 

        objtitle=jsonObject.getJSONObject("_links"); 

        JSONArray imgJsonArray=objtitle.getJSONArray("wp:featuredmedia"); 

        JSONObject objJsonImg=imgJsonArray.getJSONObject(0); 

        StringRequest request1= new StringRequest(com.android.volley.Request.Method.GET,objJsonImg.getString("href"), new Response.Listener<String>() { 

         @Override 
         public void onResponse(String response) { 
          try { 

           JSONObject jsonObject= new JSONObject(response); 
           JSONObject imggild=jsonObject.getJSONObject("guid"); 
           String rendered=imggild.getString("rendered"); 

           latestsphone.setLink(rendered); 
           latestsphone.setTitle(title); 
           mylist.add(latestsphone); 


          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 

         } 
        }); 

        RequestQueue requestQueue= Volley.newRequestQueue(getActivity()); 
        requestQueue.add(request1); 
        request1.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 


       } 

       UpcomingAdapter imgAdapter=new UpcomingAdapter(getActivity(),mylist); 
       recyclerUpcoming.setAdapter(imgAdapter); 
       recyclerUpcoming.setLayoutManager(new GridLayoutManager(getActivity(),2)); 
       recyclerUpcoming.setHasFixedSize(true); 


      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 
    requestQueue.add(stringRequest); 
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

} 
+0

在第一個請求的成功響應onResponse()上添加第二個請求 – Mundroid

回答

0

這是您正確的代碼,我糾正你的發佈代碼,使用這些作爲全局參數。

ArrayList<LatestSmartPhoneModel> mylist = new ArrayList<>(); 

String title,link;  

private void parseSmartPhone() { 

    final RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 

    StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, Config.HOMECTEGORY, new Response.Listener<String>() { 

     @RequiresApi(api = Build.VERSION_CODES.N) 
     @Override 
     public void onResponse(String response) { 

      try { 
       //getting the whole json Array from the response 
       // JSONObject jsonObject= new JSONObject(response); 

       JSONArray jsonArray = new JSONArray(response); 

       for (int i = 0; i <= 3; i++) { 
        if (i==3) 
        { 
         i=4; 
        } 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 

        JSONObject objtitle = jsonObject.getJSONObject("title"); 

        title = objtitle.getString("rendered"); 
        link=jsonObject.getString("link"); 
        objtitle = jsonObject.getJSONObject("_links"); 
        final LatestSmartPhoneModel latestsphone = new LatestSmartPhoneModel(); 
        latestsphone.setTitle(title); 
        latestsphone.setLink(link); 

        JSONArray imgJsonArray = objtitle.getJSONArray("wp:featuredmedia"); 

        JSONObject objJsonImg = imgJsonArray.getJSONObject(0); 

        StringRequest request1 = new StringRequest(com.android.volley.Request.Method.GET, objJsonImg.getString("href"), new Response.Listener<String>() { 

         @Override 
         public void onResponse(String response) { 
          try { 


           JSONObject jsonObject = new JSONObject(response); 
           JSONObject imggild = jsonObject.getJSONObject("guid"); 
           String rendered = imggild.getString("rendered"); 

           latestsphone.set_links(rendered); 

           mylist.add(latestsphone); 

           UpcomingAdapter imgAdapter = new UpcomingAdapter(getActivity(), mylist); 

           recyclerUpcoming.setAdapter(imgAdapter); 
           recyclerUpcoming.setLayoutManager(new GridLayoutManager(getActivity(), 2)); 
           recyclerUpcoming.setHasFixedSize(true); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 

         } 
        }); 
        requestQueue.add(request1); 
        request1.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 

    requestQueue.add(stringRequest); 
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

}