我如何使用curl --digest对API进行身份验证

时间:2015-07-06 13:01:51

标签: android image-recognition

我使用moodstocks识别图像, 我现在只想要如何转换此代码:

curl --digest -u YourApiKey:YourApiSecret \
"http://api.moodstocks.com/v2/echo/?foo=bar&bacon=chunky"

到Android

https://moodstocks.com/docs/http-api/how-to-use-the-api/#adding-or-removing-reference-images

我可以用Python或PHP做到这一点,但我没有找到Android的任何代码

import moodstocks
C = moodstocks.APIClient("YourApiKey", "YourApiSecret")

谢谢你的建议

1 个答案:

答案 0 :(得分:0)

您是否尝试过官方文档?

https://moodstocks.com/docs/android/getting-started-large-image-database/

似乎很好地解释了。

import android.app.Activity;
import android.os.Bundle;

import com.moodstocks.android.Scanner;
import com.moodstocks.android.MoodstocksError;

public class MainActivity extends Activity {

  // Moodstocks API key/secret pair
  private static final String API_KEY    = "your_api_key";
  private static final String API_SECRET = "your_api_secret";

  private boolean compatible = false;
  private Scanner scanner;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    compatible = Scanner.isCompatible();
    if (compatible) {
      try {
        scanner = Scanner.get();
        String path = Scanner.pathFromFilesDir(this, "scanner.db");
        scanner.open(path, API_KEY, API_SECRET);
      } catch (MoodstocksError e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (compatible) {
      try {
        scanner.close();
        scanner.destroy();
        scanner = null;
      } catch (MoodstocksError e) {
        e.printStackTrace();
      }
    }
  }
}