| | |
| | | import android.util.Log |
| | | import android.view.View |
| | | import androidx.core.content.ContextCompat |
| | | import cn.sinata.rxnetty.LogHelper |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import com.driver.igotrip.R |
| | | import com.driver.igotrip.base.Const |
| | |
| | | import com.google.android.gms.maps.model.PolylineOptions |
| | | import com.google.gson.Gson |
| | | import com.tbruyelle.rxpermissions2.RxPermissions |
| | | import io.reactivex.Observable |
| | | import io.reactivex.android.schedulers.AndroidSchedulers |
| | | import io.reactivex.schedulers.Schedulers |
| | | import okhttp3.MediaType.Companion.toMediaTypeOrNull |
| | | import okhttp3.OkHttpClient |
| | | import okhttp3.Request |
| | |
| | | return false |
| | | } |
| | | |
| | | fun getClient(url: String, function: (String) -> Unit) { |
| | | fun getClient(url: String, retryCount: Int = 3, function: (String) -> Unit) { |
| | | val client = OkHttpClient().newBuilder().build() |
| | | val mediaType = "text/plain".toMediaTypeOrNull() |
| | | val body = RequestBody.create(mediaType, "") |
| | | val request: Request = Request.Builder() |
| | | .url(url) |
| | | .method("GET", null) |
| | | .build() |
| | | Thread { |
| | | try { |
| | | val response = client.newCall(request).execute() |
| | | Handler(Looper.getMainLooper()).post { |
| | | function(response.body?.string() ?: "") |
| | | LogHelper.logRecord("请求谷歌服务器 $url") |
| | | val subscribe = Observable.just(request) |
| | | .map { request -> client.newCall(request).execute().body?.string() ?: "" } |
| | | .retry(retryCount.toLong()) // 最多额外重试 3 次(加上首次共 4 次尝试) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe({ |
| | | try { |
| | | if (it.isEmpty()) { |
| | | LogHelper.logRecord("请求谷歌服务器成功 获取结果异常为空") |
| | | return@subscribe |
| | | } |
| | | function(it) |
| | | LogHelper.logRecord("请求谷歌服务器成功") |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | LogHelper.logRecord("请求谷歌服务器成功,获取结果异常报错 message:${e.message},stackTrace:${e.stackTraceToString()}") |
| | | } |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | }.start() |
| | | |
| | | }, { throwable -> |
| | | throwable.printStackTrace() |
| | | LogHelper.logRecord("请求谷歌服务器异常报错 message:${throwable.message},stackTrace:${throwable.stackTraceToString()}") |
| | | }) |
| | | } |
| | | |
| | | /*** |