All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vip.ipav.okhttp.builder.PutBuilder Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package vip.ipav.okhttp.builder;

import okhttp3.*;
import vip.ipav.okhttp.OkHttpClientTools;
import vip.ipav.okhttp.callback.MyCallback;
import vip.ipav.okhttp.response.IResponseHandler;
import vip.ipav.okhttp.util.RegularUtils;

import java.io.IOException;
import java.util.Map;

/**
 * put builder
 * Created by tsy on 16/12/06.
 */
public class PutBuilder extends OkHttpRequestBuilderHasParam {
    private String mJsonParams = "";

    public PutBuilder(OkHttpClientTools okHttpClientTools){
        super(okHttpClientTools);
    }

    /**
     * json格式参数
     * @param json
     * @return
     */
    public PutBuilder jsonParams(String json) {
        this.mJsonParams = json;
        return this;
    }

    @Override
    public void enqueue(IResponseHandler responseHandler) {
        try {
            if(mUrl == null || mUrl.length() == 0) {
                throw new IllegalArgumentException("url can not be null !");
            }

            Request.Builder builder = new Request.Builder().url(mUrl);
            appendHeaders(builder, mHeaders);

            if (mTag != null) {
                builder.tag(mTag);
            }

            if(mJsonParams.length() > 0) {//上传json格式参数
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), mJsonParams);
                builder.put(body);
            } else {//普通kv参数
                FormBody.Builder encodingBuilder = new FormBody.Builder();
                appendParams(encodingBuilder, mParams);
                builder.put(encodingBuilder.build());
            }

            Request request = builder.build();

            mOkHttpClientTools.getOkHttpClient()
                    .newCall(request)
                    .enqueue(new MyCallback(responseHandler));
        } catch (Exception e) {
            responseHandler.onFailure(0, e.getMessage());
        }
    }

    //append params to form builder
    private void appendParams(FormBody.Builder builder, Map params) {

        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                builder.add(key, params.get(key));
            }
        }
    }

    /**
     * 同步执行
     * @return
     */
    public Response execute() {
        Request.Builder builder = new Request.Builder().url(mUrl);
        appendHeaders(builder, mHeaders);

        if (mTag != null) {
            builder.tag(mTag);
        }

        if (mJsonParams.length() > 0) {
            RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), mJsonParams);
            builder.put(body);
        } else {
            FormBody.Builder encodingBuilder = new FormBody.Builder();
            appendParams(encodingBuilder, mParams);
            builder.put(encodingBuilder.build());
        }
        Request request = builder.build();
        try {
            return mOkHttpClientTools.getOkHttpClient()
                    .newCall(request)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy