首页 > 解决方案 > 问题通过改造获得产品(Woocommerce 网站)

问题描述

我想从网站获取所有产品,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!!
    }

  }
}   

类 api接口

   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())
                        }

                    }
                }

            })
}

错误日志。请帮我。

>E/********#error#*******null: retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall@308939e

>E/----------response---------: Response{protocol=h2, code=401, message=,  url=https://refahshahrvand.com/wp-json/wc/v3/products}

标签: androidkotlinwoocommerceretrofitgetmethod

解决方案


原因可能如下——

  1. 由于您收到 HTTP 错误代码 401,即未经授权,这意味着令牌无效且 API 端点不匹配

  2. 模型类和响应中的键不匹配


推荐阅读