Interceptor 和 Retrofit 我做错了什么?
What am I doing wrong with Interceptor and Retrofit?
我已经在这个问题上坐了1天多了,我无法理解问题是什么。我想从 server.The 服务器获取用户名等待我的令牌和 returns 用户数据
API
@GET("/users/profile/")
Call<UserProfile> getProfile();
改造和 OKHTTPCLIENT
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
SharedPreferences preferences = App.Companion.getInstance().getSharedPreferences("userInfo", AppCompatActivity.MODE_PRIVATE);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(new AccessTokenInterceptor(preferences))
.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
拥有 CLASS 拦截器
class AccessTokenInterceptor constructor(
private val preferences: SharedPreferences) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = chain.run {
val token: String? = preferences.getString("access_token", null)
proceed(
request()
.newBuilder()
.addHeader("Authorization",token.toString())
.build()
)
}
}
class 我试图捕捉响应的地方。这是我的第二个问题。这个class还需要吗?
class ProfileRepository(application: Application) {
val liveDataProfile = MutableLiveData<String>()
var application: Application? = application
fun getProfileInfo(): LiveData<String> {
val call: Call<UserProfile>? = NetworkService.getInstance()
.jsonApi
.getProfile()
call?.enqueue(object : Callback<UserProfile>{
override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) {
if (response.isSuccessful) {
response.body()?.let {
liveDataProfile.value = it.username
Log.i("LogProfile","Мы получили имя пользователя с сервака = " + it.username)
}
}
}
override fun onFailure(call: Call<UserProfile>, t: Throwable) {}
})
return liveDataProfile
}
}
来自服务器的响应
I/okhttp.OkHttpClient: --> GET https://minesrv.ey.r.appspot.com/users/profile/
--> END GET
I/okhttp.OkHttpClient: <-- 400 https://minesrv.ey.r.appspot.com/users/profile/
删除请求前的/
。当您添加 /
时,用户将从最终的 url 中删除,从而使 url 无效。从上面的信息你给出的应该是这个问题。
@GET("users/profile/")
Call<UserProfile> getProfile();
我已经在这个问题上坐了1天多了,我无法理解问题是什么。我想从 server.The 服务器获取用户名等待我的令牌和 returns 用户数据
API
@GET("/users/profile/")
Call<UserProfile> getProfile();
改造和 OKHTTPCLIENT
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
SharedPreferences preferences = App.Companion.getInstance().getSharedPreferences("userInfo", AppCompatActivity.MODE_PRIVATE);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(new AccessTokenInterceptor(preferences))
.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
拥有 CLASS 拦截器
class AccessTokenInterceptor constructor(
private val preferences: SharedPreferences) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = chain.run {
val token: String? = preferences.getString("access_token", null)
proceed(
request()
.newBuilder()
.addHeader("Authorization",token.toString())
.build()
)
}
}
class 我试图捕捉响应的地方。这是我的第二个问题。这个class还需要吗?
class ProfileRepository(application: Application) {
val liveDataProfile = MutableLiveData<String>()
var application: Application? = application
fun getProfileInfo(): LiveData<String> {
val call: Call<UserProfile>? = NetworkService.getInstance()
.jsonApi
.getProfile()
call?.enqueue(object : Callback<UserProfile>{
override fun onResponse(call: Call<UserProfile>, response: Response<UserProfile>) {
if (response.isSuccessful) {
response.body()?.let {
liveDataProfile.value = it.username
Log.i("LogProfile","Мы получили имя пользователя с сервака = " + it.username)
}
}
}
override fun onFailure(call: Call<UserProfile>, t: Throwable) {}
})
return liveDataProfile
}
}
来自服务器的响应
I/okhttp.OkHttpClient: --> GET https://minesrv.ey.r.appspot.com/users/profile/
--> END GET
I/okhttp.OkHttpClient: <-- 400 https://minesrv.ey.r.appspot.com/users/profile/
删除请求前的/
。当您添加 /
时,用户将从最终的 url 中删除,从而使 url 无效。从上面的信息你给出的应该是这个问题。
@GET("users/profile/")
Call<UserProfile> getProfile();