尝试在 Android Studio 上实现 RapidApi
Posted
技术标签:
【中文标题】尝试在 Android Studio 上实现 RapidApi【英文标题】:Trying to implement RapidApi on Android Studio 【发布时间】:2020-01-14 18:00:12 【问题描述】:我正在尝试在 android Studio 上实现双字词典 API。 我得到的错误信息是:
I/OkHttpClient: Callback failure for call to https://twinword-twinword-bundle-v1.p.rapidapi.com/...
java.io.IOException: Unexpected code Responseprotocol=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,我认为问题是我无法弄清楚项目密钥是什么,因为我在 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。
请帮忙,我的最后一年项目需要这个...
【问题讨论】:
【参考方案1】:您似乎还没有在 rapidapi.com (https://rapidapi.com/twinword/api/word-dictionary/pricing) 上订阅 API
另外,我找不到您尝试使用的端点 (/word_example
),看起来正确的端点是 /example
。
您可以随时通过 support@rapidapi.com 与他们联系
【讨论】:
看来RapidAPI自己的API有问题,所以我必须创建自己的类来复制API。无论如何,非常感谢您的回复以上是关于尝试在 Android Studio 上实现 RapidApi的主要内容,如果未能解决你的问题,请参考以下文章
在 Android Chrome 81 上实现 NFC 不起作用