Search in sources :

Example 1 with RequestParams

use of com.lidroid.xutils.http.RequestParams in project xUtils by wyouflf.

the class HttpFragment method testUpload.

/////////////////////////////////////// other ////////////////////////////////////////////////////////////////
//@OnClick(R.id.download_btn)
public void testUpload(View view) {
    // 设置请求参数的编码
    //RequestParams params = new RequestParams("GBK");
    // 默认编码UTF-8
    RequestParams params = new RequestParams();
    //params.addQueryStringParameter("qmsg", "你好");
    //params.addBodyParameter("msg", "测试");
    // 添加文件
    params.addBodyParameter("file", new File("/sdcard/test.zip"));
    //params.addBodyParameter("testfile", new File("/sdcard/test2.zip")); // 继续添加文件
    // 用于非multipart表单的单文件上传
    //params.setBodyEntity(new FileUploadEntity(new File("/sdcard/test.zip"), "binary/octet-stream"));
    // 用于非multipart表单的流上传
    //params.setBodyEntity(new InputStreamUploadEntity(stream ,length));
    HttpUtils http = new HttpUtils();
    // 设置返回文本的编码, 默认编码UTF-8
    //http.configResponseTextCharset("GBK");
    // 自动管理 cookie
    http.configCookieStore(preferencesCookieStore);
    http.send(HttpRequest.HttpMethod.POST, "http://192.168.1.5:8080/UploadServlet", params, new RequestCallBack<String>() {

        @Override
        public void onStart() {
            resultText.setText("conn...");
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            if (isUploading) {
                resultText.setText("upload: " + current + "/" + total);
            } else {
                resultText.setText("reply: " + current + "/" + total);
            }
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            resultText.setText("reply: " + responseInfo.result);
        }

        @Override
        public void onFailure(HttpException error, String msg) {
            resultText.setText(msg);
        }
    });
}
Also used : HttpUtils(com.lidroid.xutils.HttpUtils) HttpException(com.lidroid.xutils.exception.HttpException) File(java.io.File) RequestParams(com.lidroid.xutils.http.RequestParams)

Example 2 with RequestParams

use of com.lidroid.xutils.http.RequestParams in project xUtils by wyouflf.

the class HttpFragment method testGetSync.

// 同步请求 必须在异步块儿中执行
private String testGetSync() {
    RequestParams params = new RequestParams();
    params.addQueryStringParameter("wd", "lidroid");
    HttpUtils http = new HttpUtils();
    http.configCurrentHttpCacheExpiry(1000 * 10);
    try {
        ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.GET, "http://www.baidu.com/s", params);
        //Header[] headers = responseStream.getBaseResponse().getAllHeaders();
        return responseStream.readString();
    } catch (Exception e) {
        LogUtils.e(e.getMessage(), e);
    }
    return null;
}
Also used : HttpUtils(com.lidroid.xutils.HttpUtils) RequestParams(com.lidroid.xutils.http.RequestParams) HttpException(com.lidroid.xutils.exception.HttpException) DbException(com.lidroid.xutils.exception.DbException) ResponseStream(com.lidroid.xutils.http.ResponseStream)

Example 3 with RequestParams

use of com.lidroid.xutils.http.RequestParams in project xUtils by wyouflf.

the class HttpFragment method testPost.

//@OnClick(R.id.download_btn)
public void testPost(View view) {
    RequestParams params = new RequestParams();
    params.addQueryStringParameter("method", "mkdir");
    params.addQueryStringParameter("access_token", "3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414");
    params.addBodyParameter("path", "/apps/测试应用/test文件夹");
    HttpUtils http = new HttpUtils();
    http.send(HttpRequest.HttpMethod.POST, "https://pcs.baidu.com/rest/2.0/pcs/file", params, new RequestCallBack<String>() {

        @Override
        public void onStart() {
            resultText.setText("conn...");
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            resultText.setText(current + "/" + total);
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            resultText.setText("upload response:" + responseInfo.result);
        }

        @Override
        public void onFailure(HttpException error, String msg) {
            resultText.setText(msg);
        }
    });
}
Also used : HttpUtils(com.lidroid.xutils.HttpUtils) HttpException(com.lidroid.xutils.exception.HttpException) RequestParams(com.lidroid.xutils.http.RequestParams)

Example 4 with RequestParams

use of com.lidroid.xutils.http.RequestParams in project xUtils by wyouflf.

the class HttpRequest method setRequestParams.

public void setRequestParams(RequestParams param, RequestCallBackHandler callBackHandler) {
    if (param != null) {
        if (uriCharset == null) {
            uriCharset = Charset.forName(param.getCharset());
        }
        List<RequestParams.HeaderItem> headerItems = param.getHeaders();
        if (headerItems != null) {
            for (RequestParams.HeaderItem headerItem : headerItems) {
                if (headerItem.overwrite) {
                    this.setHeader(headerItem.header);
                } else {
                    this.addHeader(headerItem.header);
                }
            }
        }
        this.addQueryStringParams(param.getQueryStringParams());
        HttpEntity entity = param.getEntity();
        if (entity != null) {
            if (entity instanceof UploadEntity) {
                ((UploadEntity) entity).setCallBackHandler(callBackHandler);
            }
            this.setEntity(entity);
        }
    }
}
Also used : UploadEntity(com.lidroid.xutils.http.client.entity.UploadEntity) HttpEntity(org.apache.http.HttpEntity) RequestParams(com.lidroid.xutils.http.RequestParams)

Aggregations

RequestParams (com.lidroid.xutils.http.RequestParams)4 HttpUtils (com.lidroid.xutils.HttpUtils)3 HttpException (com.lidroid.xutils.exception.HttpException)3 DbException (com.lidroid.xutils.exception.DbException)1 ResponseStream (com.lidroid.xutils.http.ResponseStream)1 UploadEntity (com.lidroid.xutils.http.client.entity.UploadEntity)1 File (java.io.File)1 HttpEntity (org.apache.http.HttpEntity)1