• xutils3的使用


    1.Xutils3 get请求

               x.Ext.init(getApplication());
         
            RequestParams requestParams = new RequestParams("URL");
            requestParams.setAsJsonContent(true);
            x.http().get(requestParams,new A());    
    class A implements Callback.CommonCallback<String>{
            @Override
            public void onSuccess(String result) {
           
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
    
            }
            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        }

    2.Xutils post请求

                 final JSONObject jsonObject=new JSONObject();
                            try {
                                jsonObject.put("name",user);
                                jsonObject.put("pwd",pwd);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                            RequestParams requestParams=new RequestParams(Constant.URL_LOGIN());
                            requestParams.setAsJsonContent(true);
                            requestParams.setBodyContent(jsonObject.toString());
                            x.http().post(requestParams, new org.xutils.common.Callback.CommonCallback<String>() {
    
                                @Override
                                public void onSuccess(String result) {
                                  
                                }
    
                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {
                                    Log.e(TAG, "onError: "+ex.toString() );
                                }
    
                                @Override
                                public void onCancelled(CancelledException cex) {
    
                                }
    
                                @Override
                                public void onFinished() {
    
                                }
                            });

    3. Xutils3 POST上传图片

         RequestParams requestParams = new RequestParams(URL);
           
                        File file = new File(absolutePath, imagefileName1);
                        requestParams.addBodyParameter("file", file);
                        requestParams.setMultipart(true);
                        x.http().post(requestParams, new A());     

    4.Xutils3多文件上传

        public void upload(){
    
    
            RequestParams requestParams=new RequestParams("http://192.168.2.104:8088/jwdLY/u/PKUx4B6d");
           /** * 
              上传二个图片 myfiles  和三个字段参数
             * */
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/AA", "1.png");
            File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/AA", "2.png");
            requestParams.addBodyParameter("myfiles", file);
            requestParams.addBodyParameter("myfiles", file1);
            /**
             * username:李四   account:nanjing   pwd:qwe
             * */
            requestParams.addBodyParameter("username", "李四");
            requestParams.addBodyParameter("account", "nanjing");
            requestParams.addBodyParameter("pwd", "qwe");
    
            requestParams.setMultipart(true);
            x.http().post(requestParams, new A());
        }
    
    
    class A implements Callback.CommonCallback<String>{
    
        @Override
        public void onSuccess(String result) {
            Log.d(TAG, "onSuccess: "+result);
    
        }
    
        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            Log.d(TAG, "onError: "+ex.toString());
    
        }
    
        @Override
        public void onCancelled(CancelledException cex) {
            Log.d(TAG, "onCancelled: "+cex.toString());
    
        }
    
        @Override
        public void onFinished() {
            Log.d(TAG, "onFinished: "+"ok");
    
        }
    }

    5.xutils3 下载文件[带进度条]参数为: url和保存路径

     x.Ext.init(getApplication());
    path = Environment.getExternalStorageDirectory() + "/update.apk";

    private ProgressDialog progressDialog;


    private void downloadFile(final String url, String path) { progressDialog = new ProgressDialog(this); RequestParams requestParams = new RequestParams(url); requestParams.setSaveFilePath(path); x.http().get(requestParams, new Callback.ProgressCallback<File>() { @Override public void onWaiting() { } @Override public void onStarted() { } @Override public void onLoading(long total, long current, boolean isDownloading) { progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("亲,努力下载中。。。"); progressDialog.show(); progressDialog.setMax((int) total); progressDialog.setProgress((int) current); } @Override public void onSuccess(File result) { Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show(); progressDialog.dismiss(); } @Override public void onError(Throwable ex, boolean isOnCallback) { ex.printStackTrace(); Toast.makeText(MainActivity.this, "下载失败,请检查网络和SD卡", Toast.LENGTH_SHORT).show(); progressDialog.dismiss(); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); }

    5.Xutils2.+下载文件

    public void download() {
    
          
            String path1="http://gdown.baidu.com/data/wisegame/6f9153d4a8d1f7d8/QQ.apk";
            String path2="http://cdn.market.hiapk.com/data/upload/2012/12_09/22/cn.lgx.phoneexpert_221804.apk";
            String path="http://gdown.baidu.com/data/wisegame/baidusearch_Android_10189_1399k.apk";
            HttpUtils utils = new HttpUtils();
            utils.download(path, target, new RequestCallBack<File>() {
    
                @Override
                public void onLoading(long total, long current, boolean isUploading) {
                    super.onLoading(total, current, isUploading);
                    System.out.println("current:" + current + "/" + total);
                }
                @Override
                public void onSuccess(ResponseInfo<File> arg0) {
                    System.out.print("****" + arg0.toString());
                    System.out.println("下载成功");
                    String path = Environment.getExternalStorageDirectory() + "/update.apk";
                    File file = new File(path);
                    
                }
    
                @Override
                public void onFailure(HttpException arg0, String arg1) {
                   
                    System.out.println("下载失败");
                }
            });
    
        }
    今天多一点积累,明天少一分烦恼
  • 相关阅读:
    01(b)无约束优化(准备知识)
    01(a)一元函数_多元函数_无约束极值问题的求解
    谱聚类
    分类算法
    Implementing EM for Gaussian mixtures
    0-1背包问题1
    ML_推荐系统与降维
    Machine Learning: Clustering & Retrieval机器学习之聚类和信息检索(框架)
    Linux命令
    Udacity_机器学习
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/6296595.html
Copyright © 2020-2023  润新知