android read textfile from web

323 Views Asked by At

Im attempting to read a text file from my onedrive, I need to check the version of the database and update if necessary.

This is an example of my code:

 private void checkVersion() {

    try {

        int dbversion = prefs.getInt("dbversion", 1);
        int dblastversion;


        URL url = new URL("");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;

        str = in.readLine();

        in.close();

        System.out.println(str);

        dblastversion = Integer.valueOf(str);

        if (dbversion < dblastversion)
            System.out.println("updates available");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

When I try to run the app crash and I got this error from logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.example/com.ex.example.ActMenu}: android.os.NetworkOnMainThreadException

On this line

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

Somebody know what is the problem? Or its better use another cloud to stor the textfile. Thanks for the help.

UPDATE 2

Ok as GPRathour said I updated my code to this:

class Getversion_Async extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        System.out.println("starting");
        try {

            URL url = new URL("https://onedrive.live.com/redir?resid=b9186f8cb138a030!56556&authkey=!AFmrzOGv_OMArzo&ithint=file%2ctxt");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            str = in.readLine();

            in.close();

            System.out.println(str);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(final Void unused) {

         System.out.println("ended");

    }
}

And I call in th onCreate like this to test:

 new Getversion_Async().execute();

Ok this code runs fine I use a site to host the file and the link ends with .txt and works fine but I cant edit the file. Somebody know how I can do this with onedrive?

1

There are 1 best solutions below

2
On BEST ANSWER

The exception clearly says android.os.NetworkOnMainThreadException. To read file from server you need to perform some Network operation and it is not a good practice to perform it on Main Thread / UI Thread as it will hang the UI till the operation is performed.

What you need to do is, run this in AsyncTask

class LogoutUser_Async extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            // Do your network task here

            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {

            // Process the result

        }
}