xUtils的基本使用(get,post,上传文件)

【原创 转载请标明出处】

xUtils已经是熟的不能在熟了。我在这里总结一下xUitls的一些基本使用。

首先导入grandle:

compile 'org.xutils:xutils:3.3.30'
然后在一个Application类中初始化。

x.Ext.init(this);

1:GET网络请求数据

  RequestParams requestParams = new RequestParams("网络请求地址");
        x.http().get(requestParams, new Callback.CacheCallback() {
            @Override
            public void onSuccess(JSONObject result) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(JSONObject result) {
                return false;
            }
        });
2:POST网络请求

RequestParams requestParams = new RequestParams("");
        requestParams.addBodyParameter("baby","");
        x.http().post(requestParams, new Callback.CacheCallback() {
            @Override
            public boolean onCache(JSONObject result) {
                return false;
            }

            @Override
            public void onSuccess(JSONObject result) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
3:上传文件

public void uploadImage(String path, boolean isTeacher) {
        mMvpView.showLoadingProgress(true);
        String temp = "Fupload_baby_photo.asp";
        RequestParams params = new RequestParams(RetrofitUtils.BASE_URL + temp);
        // 添加到请求body体的参数, 只有POST, PUT, PATCH, DELETE请求支持.
        // params.addBodyParameter("wd", "xUtils");
        // 使用multipart表单上传文件
        params.setMultipart(true);
        params.addBodyParameter("token", WineApplication.getInstance().getToken());
        params.addBodyParameter("pk_baby_id",WineApplication.getInstance().getBaby().getPk_baby_id());
        File img = new File(path);
        params.addBodyParameter("file1", img, "multipart/form-data"); // 如果文件没有扩展名, 最好设置contentType参数.
        x.http().post(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(JSONObject result) {
                KLog.json(result.toString());
                if (result.has("state")) {
                    try {
                        int state = Integer.valueOf(result.getString("state"));
                        switch (state) {
                            case 0:
                                String name = result.getString("filename");
                                if (!StringUtils.isEmpty(name)) {
                                    mMvpView.updateHeader(name);
                                }
                                mMvpView.showMessage("头像保存成功");
                                break;
                            case 1:
                                mMvpView.onTokenError();
                                break;
                            case 2:
                                break;
                            case 3:
                                break;
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                mMvpView.showLoadingProgress(false);
            }

        });
    }
git:

https://github.com/wyouflf/xUtils





你可能感兴趣的:(Android)