Skip to content

Commit

Permalink
use http below 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
leolin310148 committed Nov 8, 2016
1 parent 75b9be6 commit 5d2f4aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
30 changes: 23 additions & 7 deletions app/src/main/kotlin/me/leolin/isbadgeworking/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
)
Expand Down Expand Up @@ -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.")
}
}

Expand Down

0 comments on commit 5d2f4aa

Please sign in to comment.