0

There is my code and action after sink.close(); should change text of the textview but it isn't happening. Why?

protected void runNet() throws IOException {
        try{
        final OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("http://uprojects.pl/radiowezel/questions.db")
                .build();
        Response response = client.newCall(request).execute();
            QuestionsDB db = new QuestionsDB(context);
        File downloadedFile = new File(context.getDatabasePath("questions.db"), "");

        BufferedSink sink = Okio.buffer(Okio.sink(downloadedFile));
        sink.writeAll(response.body().source());

        sink.close();
        status.setText("Baza została zaktualizowana");


    }catch(UnknownHostException e){
        status.setText ("Brak połączenia przy pobieraniu");
    }
}
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
Yas
  • 351
  • 2
  • 17

1 Answers1

1

You are trying to update UI from background thread which is not allowed in Android, you should update it from main thread.

You can use runOnUiThread method (part of Activity class)

private void updateUi(final String string) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            status.setText(string);
        }
    });
}

you can use followed method, call it from background thread like this:

updateUi("Baza została zaktualizowana");

Or use AsyncTask which has callback which runs after execution on main thread - AsyncTask Android example

Also you can use Handler - Running code in main thread from another thread

Community
  • 1
  • 1
Vasyl Glodan
  • 556
  • 1
  • 6
  • 22