-1

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45

2 Answers2

1

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());
}
0

According to your exception Caused by java.io.IOException: Cleartext HTTP traffic to 10.0.2.2 not permitted you are trying to access your endpoint over HTTP, which is disabled for security reasons by default on android.

See this answer on a similar post for how to disable this feature, which is okay for development purposes.

But please, if you want to release your program in any way, don't change this setting and make your API available over HTTPS as well.

Dalex
  • 61
  • 1
  • 5