Я хочу получить все продукты с сайта woocommer
, но каждый раз, когда я вхожу в приложение и получаю токен, выдается ошибка 401. Мой фрагмент кода упоминается ниже
class retrofit
class ApiClient {
companion object {
val BASE_URL = "https://refahshahrvand.com/wp-json/"
private var retrofit:Retrofit? = null
private var client:OkHttpClient? = null
fun getClient() : Retrofit {
client= OkHttpClient.Builder()
.readTimeout(100,TimeUnit.SECONDS)
.connectTimeout(100,TimeUnit.SECONDS)
.build()
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
}
class apiInterface
interface ApiInterface {
@POST("wc/v3/products")
@FormUrlEncoded
fun getNamePro ( @Header("token") token: String,@Field("consumer_key") consumer_key:
String,@Field("consumer_secret") consumer_secret: String): Call<List<AllProducts>>
}
этот метод вызова get
private fun AllProductList(){
var token2: String
res = Respon()
autologin = Autologin(context)
token2 = autologin?.getToken().toString()
val apiInterface = ApiClient.getClient().create(ApiInterface::class.java)
val call : Call<List<AllProducts>> = apiInterface.getNamePro(token2, res?.consumer_key.toString(), res?.consumer_Secret.toString() )
call.enqueue(
object : Callback<List<AllProducts>> {
override fun onFailure(call: Call<List<AllProducts>>, t: Throwable) {
Log.e("*****-onFailure-*****" + t.toString(), call.toString())
}
override fun onResponse(call: Call<List<AllProducts>>, response: Response<List<AllProducts>>) {
val pr = response.body()
if (response != null) {
//adding items in list
for (i in 0..9) {
list_Prod.add(pr?.get(i)?.getName().toString())
}
mRecyclerView = view?.findViewById(R.id.Prod_recycler_view)
var mLayoutManager = LinearLayoutManager(view?.context, LinearLayoutManager.HORIZONTAL, false) as LinearLayoutManager
mRecyclerView!!.layoutManager = mLayoutManager
mAdapter = ProductsAdapter(list_Prod)
mRecyclerView!!.adapter = mAdapter
if (response.isSuccessful) {
Login.STATE_LOGIN = true
Log.e("*****-isSuccessful-****" + pr?.get(1)?.getName().toString(), response.toString())
for(i in 0..list_Prod!!.size -1){
Toast.makeText(context , pr?.get(i)?.getName().toString(),
Toast.LENGTH_LONG).show()
// Log.e("** User_display_name : " + pr?.get(i)?.getName().toString(), response.toString())
}
} else {
Log.e("********#error#*******" + pr.toString(), call.toString())
}
}
}
})
}
Ошибка logcat , Пожалуйста, помогите мне.
>E/********#error#*******null: retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall@308939e
>E/----------response---------: Response{protocol=h2, code=401, message=, url=https://refahshahrvand.com/wp-json/wc/v3/products}