OkHttp的Get和Post

Get方法
OkHttpClient client=new OkHttpClient();
        //创建Request
        Request request1=new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
Post方法
OkHttpClient okHttpClient=new OkHttpClient();
        FormBody.Builder body=new FormBody.Builder();
        body.add("keywords",name);
        FormBody build = body.build();
        Request request=new Request.Builder()
                .url(Api.FIND).post(build).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                System.out.println("result======="+result);
                getFindJson(result);
            }
        });



你可能感兴趣的:(OkHttp的Get和Post)