Simple use of Okhttp

In the project, we need to interact with the server a lot, and the most frequent ones are the Get method and the Post method. The following is the mainstream network framework Okhttp.

Let's introduce the use of Get method and Post method:

1.Get way:

OkHttpClient okHttpClient = new OkHttpClient
                    .Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10,TimeUnit.SECONDS)
                    .writeTimeout(10,TimeUnit.SECONDS)
                    .build();
            Request request = new Request
                    .Builder()
                    .url(Constants.GET_USER_ADDRESS_URL + url)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Utils.runOnUIThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DeliveryAddressActivity.this,Constants.CONNECTION_TIMEOUT,Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String s = response.body().string();
                    Log.e("add",s);
                    GetAddressListResponse getAddressListResponse =
                            GsonUtils.paserJsonToBean(s,GetAddressListResponse.class);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = getAddressListResponse;
                    handler.sendMessage(msg);
                }
            });

A few points to note: The request network timeout needs to be processed in the onFailure() method, and the Handler mechanism needs to be used after the request is successful.

2.Post method:

OkHttpClient okHttpClient = new OkHttpClient
                                .Builder()
                                .connectTimeout(10, TimeUnit.SECONDS)
                                .readTimeout(10,TimeUnit.SECONDS)
                                .writeTimeout(10,TimeUnit.SECONDS)
                                .build();
                        RequestBody requestBody = new FormBody.Builder()
                                .add("username",userName)
                                .add("oldPwdMD5",MD5Tools.md5(etOldPsw.getText().toString()).toUpperCase())
                                .add("newPwdMD5",MD5Tools.md5(etNewPsw.getText().toString()).toUpperCase())
                                .build();
                        Request requestPost = new Request.Builder()
                                .url(Constants.CHANGE_USER_PSW_URL + "appid=" + Constants.APP_ID_S +
                                "&encrypt=" + encrypt)
                                .post(requestBody)
                                .build();
                        okHttpClient.newCall(requestPost).enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Utils.runOnUIThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(ModifyPswActivity.this,Constants.CONNECTION_TIMEOUT,Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String s = response.body().string();
                                Log.e("changePsw",s);
                                ChangePswResponse changePswResponse =
                                        GsonUtils.paserJsonToBean(s,ChangePswResponse.class);
                                Message msg = new Message();
                                msg.what = 1;
                                msg.obj = changePswResponse;
                                handler.sendMessage(msg);
                            }
                        });
Special Note: The value in the request body does not need to be encoded.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325682124&siteId=291194637