guilloh
guilloh

Reputation: 31

getting data from a website Api to a android app

I have some Java knowledge but its very fresh. Yesterday I started learning some Java for Android and encountered a problem. Specifically I want to retrieve a decimal number from a website API for some in-app calculations. I have code that works perfectly in normal Java, but when I insert it into Android Studio it compiles but the app crashes at start. Any help would be appreciated.

Here's the number retrieval 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));

        }
    }

Upvotes: 3

Views: 86

Answers (1)

pavaniiitn
pavaniiitn

Reputation: 311

anything related to networking tasks you need to perform in AsyncTask only
try it in AsyncTask

Upvotes: 1

Related Questions