D
D
Deno952016-04-11 16:03:18
Android
Deno95, 2016-04-11 16:03:18

How to display OkHTTP response in TextView?

The problem with threads, since not long ago I started working with android, there were misunderstandings.
To send a Get request, I use the OkHTTP library.
Created a Get.java class:

import okhttp3.Callback;
import okhttp3.OkHttpClient;
 
public class Get {
    private final OkHttpClient client = new OkHttpClient();
 
    public void run(String url, Callback callback) {
 
        okhttp3.Request request = new okhttp3.Request.Builder()
                .url(url)
                .build();
 
        client.newCall(request).enqueue(callback);
    }
}

In MainActivity.java I call a request and try to output:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
 
import java.io.IOException;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
 
public class MainActivity extends AppCompatActivity {
    TextView  test123;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        Get test = new Get();
        test123 = (TextView) findViewById(R.id.test);
 
        test.run("https://geocode-maps.yandex.ru/1.x/?geocode=E52.091002%S23.721111",
                new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
 
                    }
 
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        @Override
                        public void run() {
 
                        }
                        test123.setText(response.body().string());
                    }
                });
    }
}

log error:

FATAL EXCEPTION: OkHttp Dispatcher
Process: com.d_code.myapplication, PID: 5292
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:972)
at android.view.View.requestLayout(View.java:16757)
at android.view. View.requestLayout(View.java:16757)
at android.view.View.requestLayout(View.java:16757)
at android.view.View.requestLayout(View.java:16757)
at android.view.View.requestLayout(View .java:16757)
at android.view.View.requestLayout(View.java:16757)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:369)
at android.view.View.requestLayout(View.java:16757)
at android.widget.TextView.checkForRelayout(TextView.java:6781)
at android.widget. TextView.setText(TextView.java:3850)
at android.widget.TextView.setText(TextView.java:3708)
at android.widget.TextView.setText(TextView.java:3683)
at com.d_code.myapplication.MainActivity$1. onResponse(MainActivity.java:33)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor .java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

Can anyone help with a solution or radically change the code so that it works?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question