Hello,
I am trying to call an API through retrofits.
The url that I call, is located on a server, in which at this moment I access go VPN.
The token I am sending right now is good.
I have tested everything via postman and it works correctly.
Instead when I call through Android studio I get the exception “retrofit unable to resolve host”.
I have made various changes to the code, according to some examples found on the net, but I can not solve the problem.
If I use the same code to access the public bees of “jsonplaceholder.typicode.com” everything works correctly
// var urlConnection: String ="http://iswvgenint03/api/"
var urlConnection: String ="http://172.20.26.375/api/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var miocontext = this.baseContext
// textViewResult= findViewById(R.id.text_view_result);
val clientBuilder = OkHttpClient.Builder()
var authTokenBearer ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJuYW1laWQiOiI2MzcxMCIsInVuaXF1ZV9uYW1lIjoiVXRpbGlzYXRldXJFdHVkZUlOb3RBY3Rlc18yNzAzNjAiLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6IjMzMkEyMDFFLUJBQUItNDRFRS1CNzYyLTM2QkY3QTRCMkE2OSIsImZhbWlseV9uYW1lIjoiaU5vdCBBY3RlcyIsInJvbGUiOlsiQWRtaW5pc3RyYXRldXJQcm9maWxzIiwiQXBwbGljYXRpb24iXSwiSU5PVEFDVEUiOiJBZG1pbiIsIklOT1RDT01QVEFCSUxJVEUiOiJnZW5hcGkiLCJOT1RJRklDQVRJT05TIjoiTk9USUZJQ0FUSU9OUyIsIlBBSUVNRU5UR0VTVElPTiI6IlBBSUVNRU5UR0VTVElPTiIsIlBBSUVNRU5UIjoiVXRpbGlzYXRldXJFdHVkZUlOb3RBY3Rlc18yNzAzNjAiLCJFU1BBQ0VDTElFTlQiOiJJTk9UQUNURSIsIkVTSUdOQVRVUkVTIjoiSU5PVEFDVEUiLCJhdWQiOiIyNzAzNjAiLCJpc3MiOiJHZW5hcGkiLCJleHAiOjE1ODcxOTY4NDQsIm5iZiI6MTU4Njg1MTI0NH0.nYf6SBlOE0AFEenWD8yLBPewyTQ9kMinCnL5eVEblZ_4CXKSbct-plvoJ7iIpugCdAiTJjiDWD4vNLIQ_ylzp93s8wctHbN2HLbZg_t-20CJwt8HQK0AmOup_jT3ayomGVOym0_ptmJNnzT6ozvmOkuYZtf5OQ0BCNuLJF4oVw1L6-KB0AXdigV-8UPbRehkuVRdTj8V1zrZqlyDGAIqDKm72UkmCtD3PTQASi428ZExSAZnfkiVF1TvloGpaeSPbhMwClX6KO5xr5ZJ9o75sCCrc8Onhtb80w43O3YH02U3PaYFd6TKL7p6ipphszg7kALeAQDSFxuUE9SJ_G3P5w"
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): okhttp3.Response? {
val original = chain.request()
val request = original.newBuilder()
.header("Authorization", authTokenBearer)
.method(original.method(), original.body())
.build()
return chain.proceed(request)
}
}
//Add the interceptor to the client builder.
clientBuilder.addInterceptor(headerAuthorizationInterceptor)
val client: OkHttpClient = clientBuilder.build()
var retrofit = Retrofit.Builder()
.baseUrl(urlConnection)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
val jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi::class.java)
val call = jsonPlaceHolderApi.getClients()
call.enqueue(object : Callback<List<Client>> {
override fun onResponse(call: Call<List<Client>>, response: Response<List<Client>>) {
if (!response.isSuccessful) {
// textViewResult.setText("Code: " + response.code())
return
}
val posts = response.body()!!
}
override fun onFailure(call: Call<List<Client>>, t: Throwable) {
Log.d("errot",t.message)
}
})
}
in an interface file I have
@GET (“Clients”)
fun getClients (): Call <List >
I arrive always in onFailure
The error messages I get are
java.net.UnknownHostException: Unable to resolve host “iswvgenint03”: No address associated with hostname
if I use the url
java.net.UnknownServiceException: CLEARTEXT communication to 172.20.26.375 not permitted by network security policy
if I use the ip address
I think android studio can’t get to the url via vpn.
Do you have any solution for me?
Thanks