【问题标题】:problem get products with retrofit (Woocommerce site)问题通过改造获得产品(Woocommerce 网站)
【发布时间】:2020-07-05 12:56:54
【问题描述】:

我想从站点 woocommer 获取所有产品,但每当我使用应用程序登录并获取令牌时都会收到 401 错误。下面提到了我的代码sn-p

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}

【问题讨论】:

    标签: android kotlin woocommerce retrofit getmethod


    【解决方案1】:

    原因可能如下-

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

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

    【讨论】:

    • 用户登录并保存令牌,所以发送令牌、客户密钥和消费者秘密值到url HTTPS,
    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2013-11-14
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多