Header Not Getting Set Properly In Android-retrofit
I'm building an app for sending access-token to the server using Retrofit in android. I don't get any error what I think should set the header correctly is not working so. The hea
Solution 1:
Your code is ok. You should add logging to your Retrofit.
// Simple add .setLogLevel(RestAdapter.LogLevel.FULL)
RestAdapter.Builderbuilder=newRestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(baseUrl)
.setClient(newOkClient(newOkHttpClient()));
I'm sure after this you will see in console:
---> HTTP POST http://10.0.2.2:8000/auth/convert-token
Accept: application/json
Authorization: Bearer facebook <user_access_token>
---> END HTTP (no body)
That means your header is set correctly.
"HTTP 405 METHOD NOT ALLOWED" may say that POST method is not expected for this resource. And maybe GET request will help you:
@GET("/convert-token")
User me();
Solution 2:
Hi Please Refer githublink and it will give some Idea,let me explain basic functionality here, Create a Interface:
publicinterfaceMyApiEndPointInterface {
/* @GET("contacts/")
Call<UserPojo> getHiveDetails();*/@FormUrlEncoded@POST("authorize/")
Call<SmsLogin> getUser(@Field("username") String username,
@Field("password") String password);
//GetProfile Details@GET("getprofile")
Call<SmsProfilePojo> getProfileDetails(@Header("Authorizations") String myTokenValue);
}
UserProfilePreview.java is An Activity,Please LookUp this Class in repo,It give basic Idea.
privatevoidgetApiCAll() {
OkHttpClientokClient=newOkHttpClient.Builder() .addInterceptor(newInterceptor() {
@Overridepublic Response intercept(Chain chain)throws IOException {
Requestrequest= chain.request().newBuilder()
.addHeader("Accept","Application/JSON").build();
return chain.proceed(request);
}
}).build();
retrofitRef = newRetrofit.Builder()
.baseUrl(BASE_URL)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
finalMyApiEndPointInterfaceservice= retrofitRef.create(MyApiEndPointInterface.class);
StringmyTokenStr="Bearer "+mToken;
Call<SmsProfilePojo> call = service.getProfileDetails(myTokenStr);
call.enqueue(newCallback<SmsProfilePojo>() {
@OverridepublicvoidonResponse(Call<SmsProfilePojo> call, retrofit2.Response<SmsProfilePojo> response) {
Log.d(TAG,"OnResponse: "+ response.code());
if(response.isSuccessful()){
SmsProfilePojoresult= response.body();
mToken = result.getToken();
txtVw_Token.setText(mToken);
}else{
Toast.makeText(getApplicationContext(),"Fail in Response",Toast.LENGTH_SHORT).show();
}
}
@OverridepublicvoidonFailure(Call<SmsProfilePojo> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
}
});
}
Post a Comment for "Header Not Getting Set Properly In Android-retrofit"