xutils网络框架的最简单使用

来源:互联网 发布:标准件网络销售好学吗 编辑:程序博客网 时间:2024/05/20 06:07

小菜鸟一枚,公司有些需求是也在学习中,希望大神提出宝贵意见,谢谢。

首先是导入依赖包:

compile 'org.xutils:xutils:3.5.0'
接下来是对于网络接口地址的初始化:
public class XHttpUrL {  public static final String FATEHER_URL = "-----------接口地址--------";

}

对于网络接口的调用

public class XHttp {    //utf-8格式转换(进行特殊格式转换调用的方法)    public static String toUtf8(String str) {        String result = null;        try {            result = URLEncoder.encode(str, "UTF-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        return result;    }
public static void getFather(String node, Callback.CommonCallback<String> listener) {////根据后台确定是否需要转换     //不用转换的方法    x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + node), listener);//用转换的方法   
x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + toutf8(node)), listener);
}
}
//在方法中调用
首先是在oncreate方法中进行初始化,不然容易空指针
x.Ext.init(getApplication());
然后直接开始调用需要的方法即可
列出几个例子:::::
普通get方法

HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
    "http://www.lidroid.com",
    new RequestCallBack<String>(){
        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            testTextView.setText(current + "/" + total);
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            textView.setText(responseInfo.result);
        }

        @Override
        public void onStart() {
        }

        @Override
        public void onFailure(HttpException error, String msg) {
        }
});






使用HttpUtils上传文件 或者 提交数据 到服务器(post方法)

RequestParams params = new RequestParams();
params.addHeader("name", "value");
params.addQueryStringParameter("name", "value");

// 只包含字符串参数时默认使用BodyParamsEntity,
// 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。
params.addBodyParameter("name", "value");

// 加入文件参数后默认使用MultipartEntity("multipart/form-data"),
// 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。
// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
// 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
params.addBodyParameter("file", new File("path"));
...

HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST,
    "uploadUrl....",
    params,
    new RequestCallBack<String>() {

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

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

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

        @Override
        public void onFailure(HttpException error, String msg) {
            testTextView.setText(error.getExceptionCode() + ":" + msg);
        }
});






原创粉丝点击