请帮助我有关okHTTP trong片段

时间:2019-07-05 08:49:40

标签: android okhttp

无法显示获取数据(khônghiểnthịđượcdữliệugetvề)

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();
        }
    }
}
}

0 个答案:

没有答案