Getting data from api site to android app

I have knowledge of Java, but it is very fresh. I started learning Java for Android yesterday and ran into a problem. Specifically, I want to get the decimal number from the website API for some in-app calculations. I have a code that works fine in regular Java, but when I paste it into Android Studio, it compiles, but the application starts from the beginning. Any help would be greatly appreciated.

Here's the number lookup code:

        URL url = new URL("api.example.com");

    URLConnection con = url.openConnection();
    InputStream is =con.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;
    int cont=0;

    while ((line = br.readLine()) != null) {
        cont++;
        if(cont==28){
            Dol = Double.parseDouble(line.substring(25, 31));

        }
    }

      

+3


source to share


1 answer


everything related to network tasks that need to be done only in AsyncTask

try inAsyncTask



+1


source







All Articles