2017-03-19 17 views
1

私はAndroidには新しく、私が作っているアプリケーションにネットワーキングの問題があります。 AsyncTaskのdoInBackgroundでAndroid(return client.recognize(new RecognitionRequest(jpeg)).get(0);)にリクエストを行うとき。Android Clarifai API com.clarifai.api.exception.ClarifaiThrottledException:あまりにも多くのリクエストがあります

com.clarifai.api.exception.ClarifaiThrottledException: TOO MANY REQUESTS.

私はdoInBackgroundclient.recognize(new RecognitionRequest(jpeg)).get(0);を呼び出していますので、それがあると仮定しますが、私はAsyncTaskの外に移動したとき、それは私に「MainThreadにあまりにも多くの仕事」を与える:しかし、私はエラーを取得しています。

私はこの問題を解決する方法がわかりませんし、助けていただければ幸いです。ありがとう!

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 

      Uri selectedImageUri = getImageUri(getApplicationContext(), photo); 
      String selectedImagePath = getPath(selectedImageUri); 

      String[] tags = new String[10]; 
      RecognitionResult result = null; 
      try { 
       result = new ClarifaiTask().execute(photo).get(); 
      } catch (ExecutionException ex) { 
      } catch (InterruptedException ex) { 
      } 
      ; 
      if (result != null) { 
       if (result.getStatusCode() == RecognitionResult.StatusCode.OK) { 
        // Display the list of tags in the UI. 
        StringBuilder b = new StringBuilder(); 
        int count = 0; 

        for (Tag tag : result.getTags()) { 
         if (count >= 10){ 
          break; 
         } 
         tags[count++] = tag.getName(); 
         System.out.println("TAG " + tag.getName()); 
        } 

       } 

      } 
     } 
    } 

private class ClarifaiTask extends AsyncTask<Bitmap, Void, RecognitionResult> { 
     @Override 
     protected RecognitionResult doInBackground(Bitmap... bitmaps) { 
      try { 
       // Scale down the image. This step is optional. However, sending large images over the 
       // network is slow and does not significantly improve recognition performance. 
       Bitmap scaled = Bitmap.createScaledBitmap(bitmaps[0], 320, 
         320 * bitmaps[0].getHeight()/bitmaps[0].getWidth(), true); 

      // Compress the image as a JPEG. 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      scaled.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      byte[] jpeg = out.toByteArray(); 

      // Send the JPEG to Clarifai and return the result. 
      return client.recognize(new RecognitionRequest(jpeg)).get(0); 
     } catch (ClarifaiException e) { 
      Log.e(TAG, "Clarifai error", e); 
      return null; 
     } 
    } 
} 

答えて

0

「あまりにも多くの要求」は、あなたが抑制されていることを示します。考えられる原因は、要求をあまりにも速く送信していることです。即時の解決策は、新しいアプリを作成してから、遅いペースでAPIを呼び出すことです。