上传

/**
     * 上传头像....上传成功之后进行显示
     * @param view
     */
    public void upload(View view) {

        //url上传的路径 file上传的文件 fileName上传文件的名称(保存在服务器上的名字)
        //params 存放除了file以外需要的参数,没有其他参数null  callBack回调
        File file = new File(Environment.getExternalStorageDirectory(),"abc.jpg");
        Map<String, String> params = new HashMap<>();
        params.put("uid","3010");


        OkHttp3Util.uploadFile("https://www.zhaoapi.cn/file/upload", file, "dash.jpg", params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){

                    //此时上传成功....获取用户信息
                    OkHttp3Util.doGet("https://www.zhaoapi.cn/user/getUserInfo?uid=3010", new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            //子线程
                            if (response.isSuccessful()){
                                String json = response.body().string();

                                //解析出icon字段...使用glide加载上传到网络上这个图片
                                final UserBean userBean = new Gson().fromJson(json, UserBean.class);


                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {

                                        Glide.with(MainActivity.this).load((String) userBean.getData().getIcon()).into(imageView);

                                    }
                                });

                            }

                        }
                    });

                }
            }
        });

    }
}




<Button
        android:onClick="upload"
        android:text="post上传"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/image_touxiang"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值