【问题标题】:retrofit2.HttpException: HTTP 302 in Androidretrofit2.HttpException:Android 中的 HTTP 302
【发布时间】:2021-10-11 23:24:39
【问题描述】:

我一直在努力解决这个问题,我正在尝试使用此代码进行改造来调用 API

interface HTTPService {

    @GET("/v1/breeds")
    suspend fun getbreeds():BreedList

}

class Retrofitclass {

    companion object{
        val BaseURL = "https://docs.thedogapi.com"
            fun getRetroInstance(): Retrofit {
                val gson = GsonBuilder()
                    .setLenient()
                    .create()

                return Retrofit.Builder()
                    .baseUrl(BaseURL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build()

            }
    }
}

在视图模型中:

  fun makeApiCall(){

        viewModelScope.launch(Dispatchers.IO){
       //  try {
                val retroinstance = Retrofitclass.getRetroInstance().create(HTTPService::class.java)
                val response = retroinstance.getbreeds()
            print("responce"+ response  )
                selectdatalist.postValue(response)
          // }  catch (e: Exception) {
            // print("error"+e.printStackTrace())
      // }
        }
    }

我尝试在 build.gradle 中添加这些代码但对我没有任何帮助或建议:

compile 'com.squareup.retrofit2:retrofit:2.0.0-SNAPSHOT'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:okhttp:3.0.1'

【问题讨论】:

    标签: android retrofit httpexception


    【解决方案1】:

    回调

    手段的改造工作
    class Retrofitclass {
    
    companion object{
        val BaseURL = "https://docs.thedogapi.com"
        fun getRetroInstance(): Retrofit {
            val gson = GsonBuilder()
                .setLenient()
                .create()
    
            return Retrofit.Builder()
                .baseUrl(BaseURL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
    
        }
    }
    

    }

    interface HTTPService {
    
    /**
    *DataClass your class the of data what represent of data request of Api
    /*
    @GET("/v1/breeds")
    suspend fun getbreeds(): Call<DataClass>
    

    }

    fun getApiBreeds {
       val retrofitClient = Retrofitclass.getRetrofit(PATH)
       val endPoint = retrofitClient.create(HTTPService::class.java)
       val callback = endPoint.getbreeds()
    
    callback.enqueue(object : Callback<DataClass> {
        override fun onResponse(
            call: Call<DataClass>,
            response: Response<DataClass>
        ) {
            if (response.isSuccessful) {
                Log.d("Response", response.body().toString())
            }
        }
    
        override fun onFailure(call: Call<DataClass>, t: Throwable) {
            t.printStackTrace()
        }
    
    })
    

    }

    //example DataClass
    

    数据类品种( var 标题:字符串, val 状态:字符串, ){}

    依赖

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    

    在 ViewModel 中:

    fun makeApiCall(){
    
        viewModelScope.launch(Dispatchers.IO){
            getApiBreeds()
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2017-06-05
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2011-07-22
      相关资源
      最近更新 更多