Home > Mobile >  Okhttp call problem
Okhttp call problem

Time:05-12

Recently to see android version 3 of the first line of code page 746, according to the book code no input, also do not complain, please master advice thank you
 
Override fun onCreate (savedInstanceState: Bundle? {
Super. OnCreate (savedInstanceState)
Val binding=ActivityHttpURLConnectionBinding. Inflate (layoutInflater)
The setContentView (binding. Root)
Binding. BtnShow. SetOnClickListener {
Log. I (TAG, "click BTN============")
Val response=HttpUtil. SendOkHttpRequest (strURL2, object: Callback {//this method call without output and no error message
Override fun onFailure (call: call, e: IOException) {
The Log i. (TAG, "11111111111111111111111")
E.p rintStackTrace ()
The Log i. (TAG, "sendOkHttpRequest Callback err: ${e. oString ()}")
}

Override fun onResponse (call: the call, the response: the response) {
The Log i. (TAG, "22222222222222222222")
Val responseData=https://bbs.csdn.net/topics/response.body? String ()//get the value
ShowResponse (responseData)
}
})
//sendRequestWithOKHttp ()//separate okhttp request method
}
}


/* *
* network operating public class
*/
The object HttpUtil {
Private val TAG="HttpUtil_wp
"Private val PORT=8000
//OKhttp request method, than ordinary simple HTTP request method
Fun sendOkHttpRequest (address: String, the callback: okhttp3. Callback) {
Val client=OkHttpClient ()
Val request=request. Builder (). The url (address). The build ()
Client. NewCall (request) equals (the callback)//internal has good child thread, in the child thread that executes the HTTP request, the results returned to the callback
}
  • Related