We love what we do and we’re good at it. Let us prove it to you.

安卓okhttp3 get请求,并更新Ui

03 Dec

安卓okhttp3 get请求,并更新Ui

public void getContent() throws IOException {
        OkHttpClient client = new OkHttpClient();
        {
            Request request = new Request.Builder().url("https://pv.sohu.com/cityjson")
                    .addHeader("User-Agent", "mozilla").build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println("onFailure: " + e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String res = response.body().string();
                    System.out.println(res);

                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 更新组件UI
                        }
                    });
                }
            });
        }

    }