API 在 android studio 中一直返回 401 但在浏览器中 returns 正常
API keeps returning 401 in android studio but returns ok in browser
我目前正在尝试通过创建一个新闻应用程序并在 https://newsapi.org 上使用其余 API 来学习 MVVM。我正在使用 retrofit2 调用 API,但我一直收到错误消息。我通过在我应该接收响应的位置放置一个断点来调试应用程序,问题是当我尝试使用 [=25] 调用 android 工作室中的 API =] key 我总是收到 401 错误,但是当我在浏览器中进行相同的调用时,我会成功。这是为什么?
这是我的代码。
API 界面
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface ApiInterface {
@GET("top-headlines")
Call<ArrayList<NewsArticles>> getNewsList(@Query("country")String country, @Query("apiKey") String
apiKey);
}
我调用的存储库。
public class Repository {
public Repository() {
}
static final String BASE_URL = "https://newsapi.org/v2/";
MutableLiveData<ArrayList<NewsArticles>> newsArticleList = new MutableLiveData<>();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<ArrayList<NewsArticles>> call = apiInterface.getNewsList("us","api");
public MutableLiveData<ArrayList<NewsArticles>> getCall() {
call.enqueue(new Callback<ArrayList<NewsArticles>>() {
@Override
public void onResponse(Call<ArrayList<NewsArticles>> call, Response<ArrayList<NewsArticles>> response) {
if (response.isSuccessful()) {
newsArticleList.setValue(response.body());
}
}
@Override
public void onFailure(Call<ArrayList<NewsArticles>> call, Throwable t) {
t.printStackTrace();
}
});
return newsArticleList;
}
}
您在这两种情况下提供的 http headers 可能存在问题。
尝试通过嗅探http协议数据进行调试,并比较两者。看看你错过了什么...
在 API 界面中明确指定您的 headers 应该有效。
public interface ApiInterface {
@Headers(
value = [
"Accept: application/json",
"Content-type:application/json"]
)
}
我目前正在尝试通过创建一个新闻应用程序并在 https://newsapi.org 上使用其余 API 来学习 MVVM。我正在使用 retrofit2 调用 API,但我一直收到错误消息。我通过在我应该接收响应的位置放置一个断点来调试应用程序,问题是当我尝试使用 [=25] 调用 android 工作室中的 API =] key 我总是收到 401 错误,但是当我在浏览器中进行相同的调用时,我会成功。这是为什么?
这是我的代码。
API 界面
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface ApiInterface {
@GET("top-headlines")
Call<ArrayList<NewsArticles>> getNewsList(@Query("country")String country, @Query("apiKey") String
apiKey);
}
我调用的存储库。
public class Repository {
public Repository() {
}
static final String BASE_URL = "https://newsapi.org/v2/";
MutableLiveData<ArrayList<NewsArticles>> newsArticleList = new MutableLiveData<>();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<ArrayList<NewsArticles>> call = apiInterface.getNewsList("us","api");
public MutableLiveData<ArrayList<NewsArticles>> getCall() {
call.enqueue(new Callback<ArrayList<NewsArticles>>() {
@Override
public void onResponse(Call<ArrayList<NewsArticles>> call, Response<ArrayList<NewsArticles>> response) {
if (response.isSuccessful()) {
newsArticleList.setValue(response.body());
}
}
@Override
public void onFailure(Call<ArrayList<NewsArticles>> call, Throwable t) {
t.printStackTrace();
}
});
return newsArticleList;
}
}
您在这两种情况下提供的 http headers 可能存在问题。
尝试通过嗅探http协议数据进行调试,并比较两者。看看你错过了什么...
在 API 界面中明确指定您的 headers 应该有效。
public interface ApiInterface {
@Headers(
value = [
"Accept: application/json",
"Content-type:application/json"]
)
}