2017-05-18 145 views
0

我正在使用makeServiceCall從TheMovieDB API獲取JSON數據。如何減慢API請求速度?

因爲他們沒有電影ID的列表,我必須循環通過ID的。

String JSONString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + 
             i + 
             "?api_key=" + API_KEY); 

I = ID

API_KEY =我的API密鑰。

當我循環訪問URL時,我得到了一些調用的IOExceptions。

I/System.out: ID: 152 
I/System.out: ID: 160 
I/System.out: ID: 165 
I/System.out: ID: 168 
I/System.out: ID: 171 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/181?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/184?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/185?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/186?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/187?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/188?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/189?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/190?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/191?api_key=b692b9da86f1cf0c1b623ea6e2770101 
E/HttpHandler: IOException: https://api.themoviedb.org/3/movie/192?api_key=b692b9da86f1cf0c1b623ea6e2770101 

一些的IOExceptions都只是URL的是回來爲NULL,因爲

{"status_code":34,"status_message":"The resource you requested could not be found."} 

我怎樣才能減緩for循環不超過API調用限制?

的HttpHandler:

package com.example.zdroa.yplex; 

import android.util.Log; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 

public class HttpHandler { 
    private static final String TAG = HttpHandler.class.getSimpleName(); 

    public HttpHandler() { 
    } 

    public String makeServiceCall(String reqUrl) { 
     String response = null; 
     try { 
      URL url = new URL(reqUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("GET"); 
      // read the response 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      response = convertStreamToString(in); 
     } catch (MalformedURLException e) { 
      Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
     } catch (ProtocolException e) { 
      Log.e(TAG, "ProtocolException: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
     return response; 
    } 
    private String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append('\n'); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 

我通話的的AsyncTask

class getWorkingIdsFromAPI extends AsyncTask<Void, Void, Void> { 

    ArrayList<String> alPersonType; 

    public getWorkingIdsFromAPI(ArrayList<String> arrayList) { 
     alPersonType = new ArrayList<>(arrayList); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     final HttpHandler httpHandler = new HttpHandler(); 

     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() { 
          @Override 
          public void run() { 
           for (int i = 0; i < 200; i++) { //20000 
            String JSONString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + i + "?api_key=" + API_KEY); 

            if (JSONString != null) { 
             try { 
              JSONObject jsonObject = new JSONObject(JSONString); 
              JSONArray jsonArray = jsonObject.getJSONArray("genres"); 

              boolean cont = true; 

              for (int a = 0; a < jsonArray.length(); a++) { 

               JSONObject jsonObject1 = jsonArray.getJSONObject(a); 
               String string = jsonObject1.getString("name"); 

               for (int b = 0; b < alPersonType.size(); b++) { 
                if (string.equals(alPersonType.get(b))) { 
                 cont = false; 
                } 
               } 
              } 

              if (cont) { 
               String id = String.valueOf(jsonObject.getInt("id")); 
               System.out.println("ID: " + id); 

               switch (id.length()) { 
                case 1: 
                 id = "0000" + id; 
                 break; 
                case 2: 
                 id = "000" + id; 
                 break; 
                case 3: 
                 id = "00" + id; 
                 break; 
                case 4: 
                 id = "0" + id; 
                 break; 
               } 
               switch (i) { 
                case 1: 
                 STRING_LIST_OF_IDS = STRING_LIST_OF_IDS + id; 
                 break; 
                default: 
                 STRING_LIST_OF_IDS = ", " + STRING_LIST_OF_IDS + id; 
                 break; 
               } 
              } 

             } catch (JSONException e) { 
              e.printStackTrace(); 
             } 
            } 
           } 
          } 
         } 

       , 3000); 


     return null; 
    } 


    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     //add STRING_LIST_OF_IDS 
     getStringOfIdsFromDB(); 
    } 
} 
+1

添加'Thread.sleep()方法'? – shmosel

+0

我不知道如何以及在哪裏,如果您將其作爲答案進行發佈並提供簡短的解釋,那麼這將對您有所幫助@shmosel –

+0

如果您自己做一點研究並解決其餘問題,則會更有幫助。 – shmosel

回答

1

你做錯了,因爲計時器任務將在每個3000毫秒後重復執行,它將執行循環中的所有請求。你可以改變你的代碼。

ArrayList<String> alPersonType; 
    i=0; // initialise your variable once instead of each time in task 

public getWorkingIdsFromAPI(ArrayList<String> arrayList) { 
    alPersonType = new ArrayList<>(arrayList); 
} 
@Override 
protected Void doInBackground(Void... params) { 


    final HttpHandler httpHandler = new HttpHandler(); 

    Timer timer = new Timer(); 
    timer.schedule(new TimerTask() { 
         @Override 
         public void run() { 
          if(i < 200) { String JSONString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + i + "?api_key=" + API_KEY); 

           if (JSONString != null) { 
            try { 
             JSONObject jsonObject = new JSONObject(JSONString); 
             JSONArray jsonArray = jsonObject.getJSONArray("genres"); 

             boolean cont = true; 

             for (int a = 0; a < jsonArray.length(); a++) { 

              JSONObject jsonObject1 = jsonArray.getJSONObject(a); 
              String string = jsonObject1.getString("name"); 

              for (int b = 0; b < alPersonType.size(); b++) { 
               if (string.equals(alPersonType.get(b))) { 
                cont = false; 
               } 
              } 
             } 

             if (cont) { 
              String id = String.valueOf(jsonObject.getInt("id")); 
              System.out.println("ID: " + id); 

              switch (id.length()) { 
               case 1: 
                id = "0000" + id; 
                break; 
               case 2: 
                id = "000" + id; 
                break; 
               case 3: 
                id = "00" + id; 
                break; 
               case 4: 
                id = "0" + id; 
                break; 
              } 
              switch (i) { 
               case 1: 
                STRING_LIST_OF_IDS = STRING_LIST_OF_IDS + id; 
                break; 
               default: 
                STRING_LIST_OF_IDS = ", " + STRING_LIST_OF_IDS + id; 
                break; 
              } 
             } 

            } catch (JSONException e) { 
             e.printStackTrace(); 
            }i++;//increase the counter 
           } 
          } 
         } 
        } 

      , 3000);return null; 
} 

你也可以有一個了Thread.sleep不同的方法()改變你的樂隊的背景像下面

@Override 
protected Void doInBackground(Void... params) { 

    final HttpHandler httpHandler = new HttpHandler(); 

          for (int i = 0; i < 200; i++) {try { 
Thread.sleep(400);} catch (InterruptedException e) { 
e.printStackTrace();} 
           String JSONString = httpHandler.makeServiceCall("https://api.themoviedb.org/3/movie/" + i + "?api_key=" + API_KEY); 

           if (JSONString != null) { 
            try { 
             JSONObject jsonObject = new JSONObject(JSONString); 
             JSONArray jsonArray = jsonObject.getJSONArray("genres"); 

             boolean cont = true; 

             for (int a = 0; a < jsonArray.length(); a++) { 

              JSONObject jsonObject1 = jsonArray.getJSONObject(a); 
              String string = jsonObject1.getString("name"); 

              for (int b = 0; b < alPersonType.size(); b++) { 
               if (string.equals(alPersonType.get(b))) { 
                cont = false; 
               } 
              } 
             } 

             if (cont) { 
              String id = String.valueOf(jsonObject.getInt("id")); 
              System.out.println("ID: " + id); 

              switch (id.length()) { 
               case 1: 
                id = "0000" + id; 
                break; 
               case 2: 
                id = "000" + id; 
                break; 
               case 3: 
                id = "00" + id; 
                break; 
               case 4: 
                id = "0" + id; 
                break; 
              } 
              switch (i) { 
               case 1: 
                STRING_LIST_OF_IDS = STRING_LIST_OF_IDS + id; 
                break; 
               default: 
                STRING_LIST_OF_IDS = ", " + STRING_LIST_OF_IDS + id; 
                break; 
              } 
             } 

            } catch (JSONException e) { 
             e.printStackTrace(); 
            } 
           } 
          } 
        ; 


    return null; 
} 
0

將一個了Thread.sleep(持續時間)內提出;在循環開始之後。

try { 
    Thread.sleep(400); 
} catch (InterruptedException e) { 
    e.printStackTrace(); 
} 

需要400毫秒。睡眠每個循環週期。