应用程序工作良好,当测试直接从Android工作室,但不是当安装为APK设备



我已经编程了一个应用程序,通过PHP/Mysqli和Retrofit登录。当我直接从Android Studio在我的智能手机上测试应用程序时(点击"运行"按钮),登录工作完美!

但是当我生成一个签名的apk然后安装在我的智能手机上时,登录不工作。我想我有一个问题与互联网连接到MySQL数据库?

我已经搜索并发现,我应该设置一个network_security_configuration。我已经设置并给予了

这个问题的解决方案是什么?我使用HTTPS/SSL连接到我自己的web服务器的php脚本(https://domain.tld/login.php)。

这是我的网络配置:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

和Android-Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uwbeinternational.weatherapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UwBeWeatherApp"
android:networkSecurityConfig="@xml/network_security_config">

这里我使用Retrofit检查登录凭据:

class RetrofitClient {
private fun getRetrofitClient(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://uwbeinternational.org/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
fun getInstance(): ApiClient {
return getRetrofitClient().create(ApiClient::class.java)
}
}

和apicclient:

interface ApiClient {
@FormUrlEncoded
@POST("android/login_service.php")
fun login(
@Field("post_username") username : String,
@Field("post_password") password : String
): Call<ResponseLogin>
}

这是proguard规则的问题。所以,我现在把这些线添加到保护。支持文件:

#Retrofit2
-keep class com.uwbeinternational.weatherapp.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

现在它工作完美!

最新更新