Как исправить ошибку подключения к сети
Написал небольшое приложение Android, которое подключается к сети чтобы считывать информацию из файла json, который находится в моем репозитории в github. На многих устройствах работает отлично, но есть также достаточно устройств (например Redmi 10, Samsung A33), которые никак не хотят подключаться к сети.
Смотрел через логгер, выдает это:
failed to connect to jdwp control socket: Connection refused
Кто-нибудь встречался с подобным? Не знаете, как решить?
Код:
@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {
@Provides
@Singleton
fun provideDataCaller() = DataCaller()
@Provides
fun provideOkHttp(): OkHttpClient {
return OkHttpClient.Builder()
// disable network cache
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
}
@Provides
@Singleton
fun provideRetrofit(client: OkHttpClient) : Retrofit {
val contentType = "application/json".toMediaType()
return Retrofit.Builder()
.client(client)
.baseUrl(Constants.BASE_URL)
.addConverterFactory(Json.asConverterFactory(contentType))
.build()
}
@Provides
@Singleton
fun provideLinksApi(retrofit: Retrofit): LinksApi = retrofit.create(LinksApi::class.java)
Добавляю Манифест по просьбе:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:requestRawExternalStorageAccess="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="31">
<activity
android:name=".ui.screens.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
Ответы (1 шт):
По советам @Style-7 добавил в манифест в раздел application:
android:networkSecurityConfig="@xml/network_security_config"
Также создал файл res/xml/network_security_config.xml с таким содержимым:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Достаточно ли это? Чтобы проверить работоспособность, нужно обращаться к пользователям, у которых выявилась такая проблема. Поэтому хочется всё уточнить и перепроверить.
Правильное ли у меня содержимое в файле network_security_config? Где то читал, что нужно указывать домены и поэтому есть сомнения.