我是 Android 新手,我正在制作的应用程序存在网络问题。当我在AsyncTask中向 Android ( return client.recognize(new RecognitionRequest(jpeg)).get(0);
)发出请求时。doInBackground
但是,我收到错误:
com.clarifai.api.exception.ClarifaiThrottledException:请求太多。
我认为这是因为我正在调用client.recognize(new RecognitionRequest(jpeg)).get(0);
,doInBackground
但是当我将它移出 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;
}
}
}