Answer the question
In order to leave comments, you need to log in
I use okHttp, the onFailure method is called in the Callback. How to fix?
class MainActivity : AppCompatActivity() {
val okHttpClient: OkHttpClient = OkHttpClient()
val request: Request = Request.Builder().get().url("http://dev-tasks.alef.im/task-m-001").build()
val call: okhttp3.Call = okHttpClient.newCall(request)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
call.enqueue(object : okhttp3.Callback{
override fun onFailure(call: okhttp3.Call, e: IOException) {
Log.d("myLog", "onFailure: ")
}
override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
Log.d("myLog", "onResponse: ")
}
})
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aleftestproject">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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.AlefTestProject">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Answer the question
In order to leave comments, you need to log in
It is very strange that after an hour (sometimes hours) of searching for a solution to the problem, after which I ask a question on Habré, the answer is found by itself and very quickly and this is not the first time. I solved my problem, but in part, you yourself will find out why later. I leave the answer for future generations. It works if onFailure gives a message similar to mine: "CLEARTEXT communication to dev-tasks.alef.im not permitted by network security policy
Information taken from here: https://coderoad.ru/59856341/OkHttp-HTTP-FAILED-ja..
In src/main/res/xml (create a folder)/ you need to create a file network_security_config.xml In the file itself ,
write this:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!--Set application-wide security config using base-config tag.-->
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>
<application
---
android:networkSecurityConfig="@xml/network_security_config">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question