Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

okhttp Updating Views on UIThread

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        // ... check for failure using `isSuccessful` before proceeding

        // Read data on the worker thread
        final String responseData = response.body().string();

        // Run view-related code back on the main thread
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    TextView myTextView = (TextView) findViewById(R.id.myTextView);
                    myTextView.setText(responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }
    }
});
Source by guides.codepath.com #
 
PREVIOUS NEXT
Tagged: #okhttp #Updating #Views #UIThread
ADD COMMENT
Topic
Name
9+8 =