我正在尝试在Android Studio上实现twinword单词词典API。
我收到的错误消息是:

I/OkHttpClient: Callback failure for call to https://twinword-twinword-bundle-v1.p.rapidapi.com/...
    java.io.IOException: Unexpected code Response{protocol=http/1.1, code=403, message=Forbidden, url=https://twinword-twinword-bundle-v1.p.rapidapi.com/word_example/?entry=mask}
        at com.example.anew.SentenceFramer$1.onResponse(SentenceFramer.java:58)
        at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
        at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:760)


我认为该消息是禁止的,因为它无法访问RapidAPI,而且我认为问题是我无法弄清什么是Project Key,因为我无法在RapidAPI网站上的任何地方找到它。还是问题出在我的代码中?

RapidApiConnect connect = new RapidApiConnect("I put my project name here", "I dont know where to find project key");
String object_1;

String host = "twinword-twinword-bundle-v1.p.rapidapi.com";
String API_KEY = "this is the api key";

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

    object_1 = getIntent().getStringExtra("First Object");
    Log.d("EDMTERROR", object_1);
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("https://twinword-twinword-bundle-v1.p.rapidapi.com/word_example/?entry=mask")
            .get()
            .addHeader("x-rapidapi-host", host)
            .addHeader("x-rapidapi-key", API_KEY)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d("EDMTERROR", "onfailure");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            Log.d("EDMTERROR", "masalah");

            try (ResponseBody responseBody = response.body()) {
                if (!response.isSuccessful()) {
                    Log.d("EDMTERROR", "unexpected code");
                    throw new IOException("Unexpected code " + response);
                }

                Headers responseHeaders = response.headers();
                Log.d("EDMTERROR", "headers");
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {

                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }

                System.out.println(responseBody.string());
            }


这是我第一次这样做,所以我对如何解决此问题一无所知。我试图访问的网站是https://rapidapi.com/twinword/api/word-dictionary?endpoint=53aa5089e4b0a798dbd1a61d

请帮忙,我的最后一年的项目需要这个...

最佳答案

看来您尚未在Rapidapi.com(https://rapidapi.com/twinword/api/word-dictionary/pricing)上订阅API

另外,我找不到您要使用的端点(/word_example),看起来正确的端点是/example

您随时可以通过[email protected]与他们联系。

07-28 03:18