From 5d2f4aab407d5c9dae6df3bf0cd3da199e28b328 Mon Sep 17 00:00:00 2001 From: leolin310148 Date: Tue, 8 Nov 2016 11:07:19 +0800 Subject: [PATCH] use http below 4.4 --- app/build.gradle | 2 +- .../me/leolin/isbadgeworking/MainActivity.kt | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6f113b8..948bbc8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { applicationId "me.leolin.isbadgeworking" minSdkVersion 15 targetSdkVersion 24 - versionCode 1 + versionCode 2 versionName "${new Date().format("yyyyMMddHHmmss")}" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/kotlin/me/leolin/isbadgeworking/MainActivity.kt b/app/src/main/kotlin/me/leolin/isbadgeworking/MainActivity.kt index a868116..f3386c8 100644 --- a/app/src/main/kotlin/me/leolin/isbadgeworking/MainActivity.kt +++ b/app/src/main/kotlin/me/leolin/isbadgeworking/MainActivity.kt @@ -5,6 +5,7 @@ import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.support.v7.app.AppCompatActivity +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -25,16 +26,23 @@ import org.jetbrains.anko.toast import retrofit2.Retrofit import retrofit2.converter.jackson.JacksonConverterFactory import java.util.* +import java.util.concurrent.TimeUnit class MainActivity : AppCompatActivity() { val isBadgeWorkingApi by lazy { Retrofit.Builder() - .baseUrl("https://api.leolin.me/is-badge-working/") + .baseUrl(if (Build.VERSION.SDK_INT >= 19) { + "https://api.leolin.me/is-badge-working/" + } else { + "http://api.leolin.me/is-badge-working/" + }) .addConverterFactory(JacksonConverterFactory.create(jacksonObjectMapper())) .client( OkHttpClient().newBuilder() + .connectTimeout(15, TimeUnit.SECONDS) + .readTimeout(15, TimeUnit.SECONDS) .addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }) .build() ) @@ -93,14 +101,22 @@ class MainActivity : AppCompatActivity() { if (send) { val dialog = indeterminateProgressDialog("Sending...") { show() } doAsync { - isBadgeWorkingApi.sendReport(req).execute().apply { + try { + isBadgeWorkingApi.sendReport(req).execute().apply { + runOnUiThread { + dialog.dismiss() + if (code() == 200) { + toast("Thank you!") + } else { + toast("Hmm..something wrong, please try later.") + } + } + } + } catch(e: Exception) { + Log.e("err", e.message, e) runOnUiThread { dialog.dismiss() - if (code() == 200) { - toast("Thank you!") - } else { - toast("Hmm..something wrong, please try later.") - } + toast("Hmm..something wrong, please try later.") } }