致命异常:OkHttp分派器进程:java.lang.BootstrapMethodError:调用站点#1引导程序方法异常

问题描述

当我尝试获取我的应用程序的api响应时,我总是收到此错误通知

致命异常:OkHttp分派器进程:com.example.githubsuser,PID:19451 java.lang.BootstrapMethodError:调用站点#1引导程序方法异常

搜索响应.kt

data class SearchResponse(
    @SerializedName("total_count")
    val totalCount: Int? = null,@SerializedName("incomplete_results")
    val incompleteResults: Boolean? = null,@SerializedName("items")
    val user: ArrayList<UserResponse>? = null
)

UserResponse.kt

 data class UserResponse(
        val name: String? = null,@SerializedName("login")
        val username: String? = null,@SerializedName("avatar_url")
        val avatarUrl: String? = null,val url: String? = null,@SerializedName("html_url")
        val htmlUrl: String? = null,@SerializedName("followers_url")
        val followersUrl: String? = null,@SerializedName("following_url")
        val followingUrl: String? = null,val company: String? = null,val location: String? = null
    )

ApiClient.kt

object ApiClient {
   private const val BASE_URL = "https://api.github.com/"

   fun getClient(): Retrofit{
      return Retrofit.Builder()
          .baseUrl(BASE_URL)
          .client(getOkHttpClient())
          .addConverterFactory(GsonConverterFactory.create())
          .build()
   }

   private fun getOkHttpClient(): OkHttpClient{
       return OkHttpClient.Builder()
           .connectTimeout(15,TimeUnit.SECONDS)
           .readTimeout(15,TimeUnit.SECONDS)
           .writeTimeout(20,TimeUnit.SECONDS)
           .addInterceptor{ chain ->
               val request = chain.request().newBuilder()
                   .addHeader("Authorization","basic $API_KEY" )
                   .build()
               chain.proceed(request)
           }
           .build()
      }
   }

NetworkRepository.kt

class NetworkRepository {
     private val apiInterface = ApiClient.getClient().create(ApiInterface::class.java)

     fun getUserSearch(username: String?): Call<SearchResponse>{
          return apiInterface.getUserSearch(username)
     }

     fun getUserDetail(username: String?): Call<UserResponse>{
         return apiInterface.getUserDetail(username)
     }

     fun getUserFollowing(username: String?): Call<UserResponse>{
         return apiInterface.getUserFollowing(username)
     }

     fun getUserFollower(username: String?): Call<UserResponse>{
         return apiInterface.getUserFollower(username)
     }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {

private lateinit var adapter :UserAdapter
private lateinit var searchResponseViewModel: SearchResponseViewModel

private fun showLoading(state: Boolean) {
    if (state) {
        progressBar.visibility = View.VISIBLE
    } else {
        progressBar.visibility = View.GONE
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    adapter = UserAdapter()

    searchResponseViewModel = ViewModelProvider(this,ViewModelProvider.NewInstanceFactory())
                                   .get(SearchResponseViewModel::class.java)

    rv_user.layoutManager = LinearLayoutManager(this@MainActivity)
    rv_user.adapter = adapter
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.main_menu,menu)
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchView = menu.findItem(R.id.search).actionView as SearchView
    searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
    searchView.queryHint = resources.getString(R.string.search_hint)
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
        override fun onQueryTextSubmit(query: String?): Boolean {
            if(!query.isNullOrEmpty()){
                searchResponseViewModel.setSearchResponseViewModel(query)
                searchResponseViewModel.getSearchResponseViewModel().observe(this@MainActivity,Observer {
                    adapter.setData(it)
                    adapter.notifyDataSetChanged()
                    showLoading(false)
                })
            }
            return true
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            return false
        }

    })
    return true
}

SearchResponseViewModel.kt

class SearchResponseViewModel: ViewModel() {
     val listSearchResponse = MutableLiveData<ArrayList<UserResponse>>()

     fun setSearchResponseViewModel(username: String?) {
         val listSearch = ArrayList<UserResponse>()
         val networkRepository = NetworkRepository()
         networkRepository.getUserSearch(username).enqueue(object : Callback<SearchResponse> {
              override fun onResponse(call: Call<SearchResponse>,response: Response<SearchResponse>){
                   val data = response.body()?.user as ArrayList<UserResponse>
                   listSearch.addAll(data)
                   listSearchResponse.postValue(listSearch)
              }

              override fun onFailure(call: Call<SearchResponse>,t: Throwable) {
                   Log.d("ViewModel",t.message,t)
              }

        })
     }
     fun getSearchResponseViewModel(): LiveData<ArrayList<UserResponse>> = listSearchResponse
 }

build.gradle

implementation "com.squareup.okhttp3:okhttp:4.8.0"
implementation "com.squareup.okhttp3:logging-interceptor:4.8.0"
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'

非常感谢您的帮助

解决方法

在应用级build.gradle中,您需要:

android {    
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...