OKHttp学习步骤

1、得到okHttpClict对象。
OkHttpClient okHttpClient = new OkHttpClient();

2、构造request

Request request = new Request.Builder()
        .get()
        .url(mBaseUrl + "login?username=shuaige&password=521")
        .build();
3、将request封装成Call
Call call = okHttpClient.newCall(request);
4、执行Call
call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        L.e("onFailure" + e.getMessage());
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        L.e("onResponse:");
        final String res = response.body().string();
        L.e(res);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv.setText(res);
            }
        });

    }
});

你可能感兴趣的:(Android)