• 上传多个或者单个文件


    public void uploadFile(){
    //192.168.1.104为本机IP地址
    //由于端口冲突:http://192.168.1.104:8080为tomcat地址
    //http://192.168.1.104为apache地址
    String url = "http://192.168.1.104:8080/FileUpload/FileUploadServlet";
    File fileOne = new File(Environment.getExternalStorageDirectory(), "tiao.mp4");
    File fileTWo = new File(Environment.getExternalStorageDirectory(), "shumei.txt");
    if (!fileOne.exists() || !fileTWo.exists()) {
    Toast.makeText(OkhttpUtilsActivity.this, "文件不存在,请修改文件路径", Toast.LENGTH_SHORT).show();
    return;
    }
    Map<String, String> paramsMap= new HashMap<>();
    //填写用户名与密码,有的服务器需要
    paramsMap.put("username", "name");
    paramsMap.put("password", "123");

    OkHttpUtils.post()
    .addFile("mFile", "server_afu.mp4", fileOne)
    .addFile("mFile", "server_test.txt", fileTWo)
    .url(url)
    .params(paramsMap)
    .build()
    .execute(new MyStringCallback());
    }
    //获取图片
    public void getPicture()
    {
    String url = "http://images.csdn.net/20150817/1.jpg";
    String url2="http://pics.sc.chinaz.com/files/pic/pic9/201807/bpic7704.jpg";
    OkHttpUtils
    .get()
    .url(url2)
    .tag(this)
    .build()
    .connTimeOut(20000)
    .readTimeOut(20000)
    .writeTimeOut(20000)
    .execute(new BitmapCallback() {
    @Override
    public void onError(Call call, Exception e, int id) {

    }

    @Override
    public void onResponse(Bitmap bitmap, int id) {
    Log.e("TAG", "onResponse:complete");
    imageView.setImageBitmap(bitmap);
    }
    });
    }
    //使用okhttp-utils上传多个或者单个文件(带参数):
    public void multiFileUpload()
    {
    HashMap<String, String> map = new HashMap<>();
    map.put("Action", "AppVersion");
    map.put("Version", "ss");
    map.put("Type", "1");

    String url = "请求接口地址" ;
    OkHttpUtils.post()
    .url(url)
    .params(map)
    .build()
    .execute(new MyStringCallback());
    }
    ?最后要在活动的onDestroy里重写取消与http的连接

    public void onPause() {
    super.onPause();
    //取消网络请求,根据tag取消请求
    OkHttpUtils.getInstance().cancelTag(this);

  • 相关阅读:
    2019ICPC徐州 H.Yuuki and a problem
    wprintf 输出中文
    bit数组
    Vs2010 Atl工程手工添加连接点
    dll非模态窗口不响应按钮消息
    VC中给控件添加ToolTip
    在Dialog中添加工具条
    在Dialog中添加状态栏
    Vc添加快捷键
    在VC中调用COM组件的方法
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11402889.html
Copyright © 2020-2023  润新知