Search in sources :

Example 1 with QARequestParams

use of cn.jeesoft.qa.libcore.http.QARequestParams in project QuickAndroid by ImKarl.

the class MainActivity method testHttp.

/**
     * 测试HTTP请求
     */
public void testHttp(View view) {
    String url = "http://www.baidu.com/s";
    QARequestParams params = new QARequestParams();
    params.putParam("wd", "android");
    QAHttpCallback<String> listener = new QASimpleHttpCallback<String>() {

        @Override
        public void onProgress(String url, long current, long total, QAHttpAction action) {
            super.onProgress(url, current, total, action);
            QAToast.show(MainActivity.this, "onProgress | " + action + " | " + current + "/" + total);
        }

        @Override
        public void onSuccessNet(String url, String data) {
            QALog.e(url, "onSuccessNet " + data);
        }

        @Override
        public void onSuccessCache(String url, String data) {
            QALog.e(url, "onSuccessCache " + data);
        }
    };
    QACore.getHttp().load(QAHttpMethod.GET, url, params, listener);
}
Also used : QAHttpAction(cn.jeesoft.qa.libcore.http.QAHttpAction) QARequestParams(cn.jeesoft.qa.libcore.http.QARequestParams) QASimpleHttpCallback(cn.jeesoft.qa.libcore.http.QASimpleHttpCallback)

Example 2 with QARequestParams

use of cn.jeesoft.qa.libcore.http.QARequestParams in project QuickAndroid by ImKarl.

the class MainActivity method testHttpFile.

/**
     * 测试HTTP请求(File)
     */
public void testHttpFile(final View view) {
    String url = "https://github.com/alafighting/QuickAndroid/archive/master.zip";
    QARequestParams params = new QARequestParams();
    params.setTargetFile(new File(QACore.file.getUsableDir(getPackageName()), "QuickAndroid-master.zip"));
    QAHttpCallback<File> listener = new QASimpleHttpCallback<File>() {

        @Override
        public void onProgress(String url, long current, long total, QAHttpAction action) {
            super.onProgress(url, current, total, action);
            QAToast.show(MainActivity.this, "onProgress | " + action + " | " + (total != -1 ? QAStringUtils.getDecimal(100D * current / total, 2) : (QAStringUtils.getDecimal(1D * current / 1024, 2) + "kb")));
        }

        @Override
        public void onSuccessNet(String url, File data) {
            QALog.e(url, "onSuccessNet", data);
        }

        @Override
        public void onSuccessCache(String url, File data) {
            QALog.e(url, "onSuccessCache", data);
        }
    };
    QACore.getHttp().load(QAHttpMethod.GET, url, params, listener);
}
Also used : QAHttpAction(cn.jeesoft.qa.libcore.http.QAHttpAction) QARequestParams(cn.jeesoft.qa.libcore.http.QARequestParams) File(java.io.File) QASimpleHttpCallback(cn.jeesoft.qa.libcore.http.QASimpleHttpCallback)

Example 3 with QARequestParams

use of cn.jeesoft.qa.libcore.http.QARequestParams in project QuickAndroid by ImKarl.

the class OkHttp method createRequest.

private <T> Request createRequest(QAHttpMethod method, String url, QARequestParams params, final QAHttpCallback<T> listener) {
    if (method == null) {
        method = QAHttpMethod.GET;
    }
    final String finalUrl = url;
    if (method == QAHttpMethod.GET) {
        url = parseGetUrl(url, params != null ? params.getParams() : null);
        params = null;
    } else {
        if (params == null) {
            params = new QARequestParams();
        }
    }
    Request.Builder builder = new Request.Builder();
    try {
        builder.url(url);
    } catch (final Exception e) {
        sendFailedCallback(finalUrl, e, listener);
        return null;
    }
    // 强制使用缓存
    builder.cacheControl(CacheControl.FORCE_CACHE);
    // header
    Headers.Builder headerBuilder = new Headers.Builder();
    if (params != null && !params.getHeaders().isEmpty()) {
        for (Entry<String, String> entry : params.getHeaders().entrySet()) {
            headerBuilder.add(entry.getKey(), entry.getValue());
        }
    }
    builder.headers(headerBuilder.build());
    // param
    RequestBody requestBody = null;
    if (params != null) {
        if (!TextUtils.isEmpty(params.getBody())) {
            requestBody = RequestBody.create(MEDIA_TYPE_TEXT, params.getBody());
        } else {
            try {
                MultipartBuilder paramBuilder = new MultipartBuilder();
                if (params != null && !params.getParams().isEmpty()) {
                    for (Entry<String, List<Part>> entry : params.getParams().entrySet()) {
                        String name = entry.getKey();
                        List<Part> parts = entry.getValue();
                        if (parts != null && !parts.isEmpty()) {
                            for (Part part : parts) {
                                paramBuilder.addPart(part.header(), part.body());
                            }
                        }
                    }
                }
                requestBody = paramBuilder.build();
            } catch (IllegalStateException e) {
                requestBody = new FormEncodingBuilder().build();
            }
        }
    }
    builder.method(method.name(), requestBody == null ? null : new ProgressRequestBody(requestBody, new OnProgressListener() {

        @Override
        public void onProgress(long currentBytes, long contentLength) {
            // 上传进度
            sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.REQUEST, listener);
        }
    }));
    final Request request = builder.build();
    mOnProgressListeners.put(request, new OnProgressListener() {

        @Override
        public void onProgress(long currentBytes, long contentLength) {
            // 下载进度
            sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.RESPONSE, listener);
        }
    });
    return request;
}
Also used : Headers(com.squareup.okhttp.Headers) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) QANullException(cn.jeesoft.qa.error.QANullException) QANoSupportException(cn.jeesoft.qa.error.QANoSupportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QAException(cn.jeesoft.qa.error.QAException) IOException(java.io.IOException) StringPart(cn.jeesoft.qa.libcore.http.part.StringPart) Part(cn.jeesoft.qa.libcore.http.part.Part) List(java.util.List) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) QARequestParams(cn.jeesoft.qa.libcore.http.QARequestParams) RequestBody(com.squareup.okhttp.RequestBody)

Example 4 with QARequestParams

use of cn.jeesoft.qa.libcore.http.QARequestParams in project QuickAndroid by ImKarl.

the class MainActivity method testHttpParser.

/**
     * 测试HTTP请求(Parser)
     */
public void testHttpParser(final View view) {
    String url = "http://www.kuaidi100.com/query";
    QARequestParams params = new QARequestParams();
    params.putParam("type", "快递公司代号");
    params.putParam("postid", "快递单号");
    /**
         * 快递单信息
         */
    class Express implements QAJsonParser<Express> {

        int status;

        String message;

        @Override
        public Express parser(QAJson data) {
            QAJsonObject root = (QAJsonObject) data;
            this.status = root.getInt("status");
            this.message = root.getString("message");
            return this;
        }

        @Override
        public String toString() {
            return "Express [status=" + status + ", message=" + message + "]";
        }
    }
    QAHttpCallback<Express> listener = new QASimpleHttpCallback<Express>() {

        @Override
        public void onProgress(String url, long current, long total, QAHttpAction action) {
            super.onProgress(url, current, total, action);
            QAToast.show(MainActivity.this, "onProgress | " + action + " | " + current + "/" + total);
        }

        @Override
        public void onSuccessNet(String url, Express data) {
            QALog.e(url, "onSuccessNet", data);
        }

        @Override
        public void onSuccessCache(String url, Express data) {
            QALog.e(url, "onSuccessCache", data);
        }
    };
    QACore.getHttp().load(QAHttpMethod.GET, url, params, new Express(), listener);
}
Also used : QAJsonObject(cn.jeesoft.qa.json.QAJsonObject) QAJson(cn.jeesoft.qa.json.QAJson) QAJsonParser(cn.jeesoft.qa.libcore.http.QAJsonParser) QAHttpAction(cn.jeesoft.qa.libcore.http.QAHttpAction) QARequestParams(cn.jeesoft.qa.libcore.http.QARequestParams) QASimpleHttpCallback(cn.jeesoft.qa.libcore.http.QASimpleHttpCallback)

Aggregations

QARequestParams (cn.jeesoft.qa.libcore.http.QARequestParams)4 QAHttpAction (cn.jeesoft.qa.libcore.http.QAHttpAction)3 QASimpleHttpCallback (cn.jeesoft.qa.libcore.http.QASimpleHttpCallback)3 QAException (cn.jeesoft.qa.error.QAException)1 QANoSupportException (cn.jeesoft.qa.error.QANoSupportException)1 QANullException (cn.jeesoft.qa.error.QANullException)1 QAJson (cn.jeesoft.qa.json.QAJson)1 QAJsonObject (cn.jeesoft.qa.json.QAJsonObject)1 QAJsonParser (cn.jeesoft.qa.libcore.http.QAJsonParser)1 Part (cn.jeesoft.qa.libcore.http.part.Part)1 StringPart (cn.jeesoft.qa.libcore.http.part.StringPart)1 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)1 Headers (com.squareup.okhttp.Headers)1 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)1 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 List (java.util.List)1