致命异常:OkHttp Dispatcher进程:java.lang.BootstrapMethodError:调用站点



当我试图为我的应用程序获取api响应时,我总是收到这个错误通知

致命异常:OkHttp Dispatcher进程:com.example.githubsuser,PID:19451java.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)
}
}

主活动.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
}
}

相关内容

  • 没有找到相关文章

最新更新