пожалуйста, помогите мне с фрагментом okHTTP - PullRequest
0 голосов
/ 05 июля 2019

не может отобразить полученные данные (không hiển thị được dữ liệu get về)

public class Gift_Fragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_gift,container,false);
    TextView textView = view.findViewById(R.id.textView12);
    GetExample example = new GetExample();
    Log.i("Main","success");

    String response = null;
    try {
        response = example.run("https://raw.githubusercontent.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/GetExample.java");
    } catch (IOException e) {
        e.printStackTrace();
    }
    textView.setText(response);
    return view;
}

public class GetExample {
    OkHttpClient client = new OkHttpClient();

    String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try(Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}
}
...