Skip to content

Commit

Permalink
修复网络造成的闪退与 UI 错误
Browse files Browse the repository at this point in the history
核心模块: 修复 OkHttp 因网络不佳抛出异常造成的闪退
UI 修复: 设置项页面添加提醒;移除错误的 onClick 参数;修复跟踪项设置页面暂时离开软件会导致确认按钮消失的问题
  • Loading branch information
xz-dev committed Dec 8, 2019
1 parent 230a60c commit 3deba10
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
applicationId "net.xzos.upgradeall"
minSdkVersion 21
targetSdkVersion 29
versionCode 30
versionName "0.1.0-rc.1"
versionCode 31
versionName "0.1.0-rc.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class JSUtils(private val logObjectTag: Pair<String, String>) {
}

fun getHttpResponse(URL: String): String? =
okHttpApi.getHttpResponse(URL)
okHttpApi.getHttpResponse(URL, catchError = false)

fun selNByJsoupXpath(userAgent: String?, URL: String, xpath: String): ArrayList<*> {
val doc = jsoupApi.getDoc(URL, userAgent = userAgent) ?: return arrayListOf<Any>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import okhttp3.JavaNetCookieJar
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
import java.net.SocketException


class OkHttpApi(private val logObjectTag: Pair<String, String>) {
Expand All @@ -15,19 +16,19 @@ class OkHttpApi(private val logObjectTag: Pair<String, String>) {

internal var requestHeaders = hashMapOf<String, String>()

fun getHttpResponse(url: String): String? =
fun getHttpResponse(url: String, catchError: Boolean = true): String? =
jsCache.getHttpResponseCache(url)
?: getRawHttpResponse(url)
?: getRawHttpResponse(url, catchError)
?.also {
jsCache.cacheHttpResponse(url, it)
}

private fun getRawHttpResponse(url: String): String? {
private fun getRawHttpResponse(url: String, catchError: Boolean): String? {
try {
Request.Builder().url(url)
} catch (e: IllegalArgumentException) {
Log.e(logObjectTag, TAG, """getHttpResponse: ${e.printStackTrace()}
|URL: $url """.trimMargin())
Log.e(logObjectTag, TAG, """getHttpResponse: URL: $url
| $e """.trimMargin())
null
}?.let { builder ->
val request = builder.build()
Expand All @@ -44,7 +45,13 @@ class OkHttpApi(private val logObjectTag: Pair<String, String>) {
}
}
}
return response?.body?.string()
return try {
response?.body?.string()
} catch (e: SocketException) {
if (!catchError) throw e
Log.e(logObjectTag, TAG, "getHttpResponse: 网络错误")
null
}
}
return null
}
Expand All @@ -54,4 +61,4 @@ class OkHttpApi(private val logObjectTag: Pair<String, String>) {
private const val TAG = "OkHttpApi"
private val okHttpClient = OkHttpClient().newBuilder().cookieJar(JavaNetCookieJar(MyCookieManager)).build()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ArrayAdapter
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -128,12 +126,16 @@ class AppSettingFragment : Fragment() {
}

override fun onPause() {
super.onPause()
SearchUtils.clearResultCache()
}

override fun onDestroy() {
super.onDestroy()
activity?.floatingActionButton?.run {
this.setOnClickListener(null)
this.visibility = View.GONE
}
super.onPause()
}

private fun setEndHelpIcon() {
Expand All @@ -159,27 +161,34 @@ class AppSettingFragment : Fragment() {
}

private fun addApp() {
val name = editName.text.toString()
val url = editUrl.text.toString()
val apiNum = apiSpinner.selectedItemPosition
val versionChecker = targetChecker
val progressBar = ProgressBar(context)
// 弹出等待框
progressBar.visibility = View.VISIBLE
activity?.window?.let {
it.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
// 添加数据库
val addRepoSuccess = addRepoDatabase(databaseId, name, apiNum, url, versionChecker)
// 取消等待框
if (addRepoSuccess) {
ServerContainer.AppManager.setApp(databaseId)
activity?.onBackPressed() // 跳转主页面
} else
Toast.makeText(context, "什么?添加失败!", Toast.LENGTH_LONG).show()
it.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
GlobalScope.launch {
val name = editName.text.toString()
val url = editUrl.text.toString()
val apiNum = apiSpinner.selectedItemPosition
val versionChecker = targetChecker
launch(Dispatchers.Main) {
// 弹出等待框
activity?.run {
this.floatingActionButton?.visibility = View.GONE
this.loadingBar?.visibility = View.VISIBLE
}
}
val addRepoSuccess = addRepoDatabase(databaseId, name, apiNum, url, versionChecker) // 添加数据库
launch(Dispatchers.Main) {
if (addRepoSuccess) {
ServerContainer.AppManager.setApp(databaseId)
activity?.onBackPressed() // 跳转主页面
} else
Toast.makeText(context, "什么?添加失败!", Toast.LENGTH_LONG).show()
// 取消等待框
activity?.run {
this.floatingActionButton?.visibility = View.VISIBLE
this.loadingBar?.visibility = View.GONE
}
}
}
}
progressBar.visibility = View.GONE
}

private fun setSettingItem() {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/fragment_app_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dip"
android:singleLine="true"
android:maxLines="1"
android:text="@string/loading"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Expand Down Expand Up @@ -88,8 +88,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:onClick="onClick"
android:singleLine="true"
android:text="@string/example_package_name"
android:textColor="@color/text_low_priority_color" />

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/layout_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@
app:srcCompat="@drawable/ic_plus"
tools:tint="@color/light_gray" />

<ProgressBar
android:id="@+id/loadingBar"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:visibility="gone" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 3deba10

Please sign in to comment.