Search in sources :

Example 1 with PostFormBuilder

use of org.yh.library.okhttp.builder.PostFormBuilder in project YhLibraryForAndroid by android-coco.

the class OkHttpRequestManager method postForm.

public void postForm(final String host, final String suffix, Map<String, String> headers, final Map<String, Object> params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
    final String url = host + suffix;
    // 组成的URL 为空或者不是http或https开头都是非法URL
    if (isEmpty(url) || (!url.startsWith(Constants.FILE_HTTP) && !url.startsWith(Constants.FILE_HTTPS))) {
        callback.onFailure(-3, "非法URL");
        callback.onFinish();
        return;
    }
    if (StringUtils.isEmpty(headers)) {
        headers = this.headers;
    }
    PostFormBuilder formbuilder = OkHttpUtils.post();
    formbuilder.url(url).tag(tag).headers(headers);
    Iterator<Entry<String, Object>> paramsIterator = params.entrySet().iterator();
    while (paramsIterator.hasNext()) {
        Entry<String, Object> entry = paramsIterator.next();
        // key
        String key = entry.getKey();
        // 值
        Object obj = entry.getValue();
        if (obj instanceof File) {
            // 如果是文件
            File newFile = (File) obj;
            formbuilder.addFile(key, newFile.getName(), newFile);
        } else if (obj instanceof String) {
            // 如果是字符
            formbuilder.addParams(key, (String) obj);
        }
    // LogUtils.e("postForm参数集:", "key:" + key + "   Value:" + obj);
    }
    formbuilder.build().connTimeOut(connTimeOut).readTimeOut(readTimeOut).writeTimeOut(// 
    writeTimeOut).execute(new StringCallback() {

        @Override
        public void onResponse(String response, int id) {
            try {
                JSONObject json = new JSONObject(response);
                String ret = json.getString(Constants.RESULT);
                if (ERROE_3001.equals(ret) || ERROE_3002.equals(ret)) {
                // 相关操作
                // EventBus.getDefault().post(
                // new EventBusBean(new Intent(
                // AppConfig.ACTION_PLOGIN_OUT)));
                // EventBus.getDefault().post(
                // new EventBusBean(new Intent(
                // AppConfig.ACTION_AGAIN_TOKEN)));
                } else {
                    callback.onSuccess(response);
                    callback.onFinish();
                }
            } catch (JSONException e) {
                callback.onSuccess(response);
                callback.onFinish();
            }
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            // LogUtils.e("OkHttpRequestManager", "postForm请求URL:" + url
            // + " 请求错误:" + e.getMessage() + "  " + id);
            dealError(e, callback);
        }

        @Override
        public void onAfter(int id) {
            super.onAfter(id);
        }
    });
}
Also used : Call(okhttp3.Call) StringCallback(org.yh.library.okhttp.callback.StringCallback) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException) Entry(java.util.Map.Entry) JSONObject(org.json.JSONObject) PostFormBuilder(org.yh.library.okhttp.builder.PostFormBuilder) JSONObject(org.json.JSONObject) File(java.io.File)

Example 2 with PostFormBuilder

use of org.yh.library.okhttp.builder.PostFormBuilder in project YhLibraryForAndroid by android-coco.

the class YHOkHttp1 method postForm.

private static void postForm(final String host, final String suffix, final Map<String, Object> params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut, final int control) {
    String prefix = "";
    Map<String, String> mapUrls = null;
    if (!isEmpty(host) && !host.equals(Constants.HOST_MAIN)) {
        // mapUrls = getHost();
        prefix = mapUrls.get(host);
    }
    if (host.equals(Constants.HOST_MAIN)) {
        prefix = Constants.HOST_MAIN;
    }
    String url = "";
    url = prefix + suffix;
    LogUtils.e("postForm请求host:", url + "   第几次请求:" + control);
    final String url1 = url;
    // 组成的URL 为空或者不是http或https开头都是非法URL
    if (isEmpty(url1) || (!url1.startsWith(Constants.FILE_HTTP) && !url1.startsWith(Constants.FILE_HTTPS))) {
        callback.onFailure(-3, "非法URL");
        callback.onFinish();
        return;
    }
    PostFormBuilder formbuilder = OkHttpUtils.post();
    formbuilder.url(url1).tag(tag);
    Iterator<Entry<String, Object>> paramsIterator = params.entrySet().iterator();
    while (paramsIterator.hasNext()) {
        Entry<String, Object> entry = paramsIterator.next();
        // key
        String key = entry.getKey();
        // 值
        Object obj = entry.getValue();
        if (obj instanceof File) {
            // 如果是文件
            File newFile = (File) obj;
            formbuilder.addFile(key, newFile.getName(), newFile);
        } else if (obj instanceof String) {
            // 如果是字符
            formbuilder.addParams(key, (String) obj);
        }
        LogUtils.e("postForm参数集:", "key:" + key + "   Value:" + obj);
    }
    formbuilder.build().connTimeOut(connTimeOut).readTimeOut(readTimeOut).writeTimeOut(// 
    writeTimeOut).execute(new StringCallback() {

        @Override
        public void onResponse(String response, int id) {
            try {
                JSONObject json = new JSONObject(response);
                String ret = json.getString(Constants.RESULT);
                if (ERROE_3001.equals(ret) || ERROE_3002.equals(ret)) {
                // 相关操作
                // EventBus.getDefault().post(
                // new EventBusBean(new Intent(
                // AppConfig.ACTION_PLOGIN_OUT)));
                // EventBus.getDefault().post(
                // new EventBusBean(new Intent(
                // AppConfig.ACTION_AGAIN_TOKEN)));
                } else {
                    callback.onSuccess(response);
                    callback.onFinish();
                }
            } catch (JSONException e) {
                callback.onSuccess(response);
                callback.onFinish();
            }
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            LogUtils.e("OkHttpRequestManager", "postForm请求URL:" + url1 + " 请求错误:" + e.getMessage() + "  " + id);
            dealError(e, callback);
        }

        @Override
        public void onAfter(int id) {
            super.onAfter(id);
        }
    });
}
Also used : Call(okhttp3.Call) StringCallback(org.yh.library.okhttp.callback.StringCallback) JSONException(org.json.JSONException) JSONException(org.json.JSONException) Entry(java.util.Map.Entry) JSONObject(org.json.JSONObject) PostFormBuilder(org.yh.library.okhttp.builder.PostFormBuilder) JSONObject(org.json.JSONObject) File(java.io.File)

Aggregations

File (java.io.File)2 Entry (java.util.Map.Entry)2 Call (okhttp3.Call)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PostFormBuilder (org.yh.library.okhttp.builder.PostFormBuilder)2 StringCallback (org.yh.library.okhttp.callback.StringCallback)2 IOException (java.io.IOException)1