Api调用适用于Volley,但不适用于Retrofit



目前我正试图改变我的Api代码从凌空改造。教程很简单,但出于某种原因,当我在Volley上做api调用时,它工作,当我在Retrofit上调用它时,它没有。在开始api调用之前,我使用Oauth2和Webview授权自己。webview给了我一个授权码,然后我尝试将其转换为access_token。下面你可以看到Volley的简单代码:

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url2 ="...oauth/token";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url2,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {    
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("grant_type" , "authorization_code");
params.put("client_id" , "...");
params.put("client_secret" , "...");
params.put("redirect_uri" , "...");
params.put("code" , code);
params.put("scope" , "*");


return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);

下面你可以看到改造代码:

接口:

@POST("oauth/token")
Call<ResponseBody> getToken(@Query("grant_type") String grantType,@Query("client_id") String clientId , @Query("client_secret") String clientSecret,
@Query("redirect_uri") String redirectURI, @Query("code") String code,@Query("scope") String scope);

BaseApi:

retrofitInstance = new Retrofit.Builder()
.baseUrl(CONSTANT_API_BASE_URL_STAGING)
.build();
apiServiceInstance = retrofitInstance.create(ApiService.class);

实现:

Call<ResponseBody> s = baseApi.apiServiceInstance.getToken("authorization_code" ,"...",
"...",
"...",
code,"*");
s.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "onResponse: "+response);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});

当我调用volley代码时,它返回一个数组作为字符串,它给了我refresh_token, access_token和expire_date

当我调用Retrofit代码时,它返回{protocol=http/1.1, code=400, message=Bad Request, url=…}

您可以看到下面的改造代码。

依赖性:

implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

基础API: -

private static Retrofit retrofit;
public static Retrofit getClient() {
OkHttpClient unsafeOkHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();
if (retrofit == null) {
retrofit =newRetrofit.Builder().baseUrl(BASEURL).client(unsafeOkHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
}
return retrofit;
}

界面:

@POST("oauth/token")
Call<ResponseBody> getToken(@Query("grant_type") String grantType,@Query("client_id") String clientId , @Query("client_secret") String clientSecret,
@Query("redirect_uri") String redirectURI, @Query("code") String code, @Query("scope") String scope);
实现:

public RequestAPI requestAPI = (RequestAPI) ApiClient.getClient().create(RequestAPI.class);
public void getResponse(){
this.requestAPI.getToken("authorization_code" ,"...",
"...",
"...",
code,"*");.enqueue(this.GetTokenCallback);
}

Callback<ResponseBody> GetCategoryCallback = new Callback<ResponseBody>() {
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
ResponseBody body = response.body();
Log.e("RESPONSE", "Token: " + new Gson().toJson(response.body()));
}
public void onFailure(Call<ResponseBody> call, Throwable th) {
Toast.makeText(activity, "Something went wrong..." + th.getMessage(), 
Toast.LENGTH_LONG).show();
}
};

相关内容

  • 没有找到相关文章

最新更新