Issue
I try to read info form my API: http://localhost:5001/Subject => In android: http://10.0.2.2:5001/Subject. And my app has crash. (App work with another link APIrestfull in Internet)
inner class API : AsyncTask<String, Void, String>(){
override fun doInBackground(vararg p0: String?): String {
var content = StringBuilder();
val url = URL(p0[0]);
var urlConnection = url.openConnection() as HttpURLConnection;
val inputStreamReader = InputStreamReader(urlConnection.getInputStream());
val bufferedReader = BufferedReader(inputStreamReader);
var line = "";
try {
do{
line=bufferedReader.readLine();
if (line!=null) content.append(line);
} while (line!= null);
bufferedReader.close();
} catch (e : Exception) {
Log.d("AAA",e.toString());
}
return content.toString();
}
override fun onPostExecute(result: String) {
super.onPostExecute(result)
Toast.makeText(applicationContext, "${result.toString()}", Toast.LENGTH_LONG).show();
}
}
My api is asp.net core webapi.
Error: My error
Solution
This is the way to fix it: (Thanks all)
if (conn instanceof HttpsURLConnection) {
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
httpsConn.setSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(0, null));
httpsConn.setHostnameVerifier(new AllowAllHostnameVerifier());
}
Answered By - Huy Loc Vu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.