美文网首页
okhttp模拟get、post请求

okhttp模拟get、post请求

作者: 我想放假休息 | 来源:发表于2019-12-02 16:09 被阅读0次

    现在 build.gradle(Module:app) 文件的 dependencies 节点下添加下面代码(导入okhttp所需要的包)

        implementation 'com.squareup.okhttp3:okhttp:3.5.0'
    
    
    import android.widget.Toast;
    
    import java.io.IOException;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.FormBody;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    public class OkHttpUtil {
    
        /**
         * 发送异步GET请求
         */
        private void getAsyncRequest() {
            //创建OkHttpClient对象
            OkHttpClient okhttpClient = new OkHttpClient();
            //创建Request对象
            Request request = new Request.Builder()
                    .url("http://22683cf223.imwork.net:27450/test1")//请求的地址,根据需求带参
                    .build();
            //创建call对象
            Call call = okhttpClient.newCall(request);
            call.enqueue(new Callback() {
                /**
                 * 请求失败后执行
                 * @param call
                 * @param e
                 */
                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(MainActivity.this,"异步get方式请求数据失败!",Toast.LENGTH_LONG).show();
                }
    
                /**
                 * 请求成功后执行
                 * @param call
                 * @param response
                 * @throws IOException
                 */
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String res = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"异步get方式请求数据成功!",Toast.LENGTH_LONG).show();
                            System.out.println(res);
                            showRequest(res);
    
                            Toast.makeText(MainActivity.this,res,Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
        }
    
        /**
         * 发送同步的get请求
         */
        public void getSyncRequest() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://hibernate.org/orm/releases/5.3/")
                            .build();
                    try {
                        Response response = okHttpClient.newCall(request).execute();
                        String responseResult = response.body().string();
                        
                        showRequest(responseResult);
                        
                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    
        /**
         * 发送异步post()请求
         */
        private void postAsynsRequest() {
            OkHttpClient okhttpClient = new OkHttpClient();
            FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
            formBody.add("usernam", "Thanlon");
            formBody.add("password", "123");
            Request request = new Request.Builder()
                    .url("https://www.baidu.com")
                    .post(formBody.build())
                    .build();
            Call call2 = okhttpClient.newCall(request);
            call2.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(MainActivity.this,"异步post请求数据失败!",Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String res = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"post异步请求数据成功!",Toast.LENGTH_LONG).show();
                            showRequest(res);
                        }
                    });
                }
            });
        }
    
        /**
         * 发送同步的post请求
         */
        public void postSyncRequest() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    FormBody.Builder formBody = new FormBody.Builder();
                    formBody.add("username", "Thanlon");
                    formBody.add("password", "123");
                    Request request = new Request.Builder()
                            .url("https://www.douban.com")
                            .post(formBody.build())
                            .build();
                    try {
                        Response response = okHttpClient.newCall(request).execute();
                        String responseResult = response.body().string();
                        showRequest(responseResult);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:okhttp模拟get、post请求

          本文链接:https://www.haomeiwen.com/subject/ubdebctx.html