使用okHttp图片上传 [英] Image upload using okHttp

查看:3425
本文介绍了使用okHttp图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传图片 okhttp ,但我无法找到MultipartBuilder为邮政Image.What我可以使用,而不是这个。

i want to upload image using okhttp but i am not able to find MultipartBuilder for Post Image.What can i use instead of this.

下面是我的code

public static JSONObject uploadImage(File file) {

        try {

            final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

            RequestBody req = new MultipartBuilder().setType(MultipartBody.FORM).addFormDataPart("userid", "8457851245")
                    .addFormDataPart("userfile","profile.png", RequestBody.create(MEDIA_TYPE_PNG, file)).build();

            Request request = new Request.Builder()
                    .url("url")
                    .post(req)
                    .build();

            OkHttpClient client = new OkHttpClient();
            Response response = client.newCall(request).execute();

            Log.d("response", "uploadImage:"+response.body().string());

            return new JSONObject(response.body().string());

        } catch (UnknownHostException | UnsupportedEncodingException e) {
            Log.e(TAG, "Error: " + e.getLocalizedMessage());
        } catch (Exception e) {
            Log.e(TAG, "Other Error: " + e.getLocalizedMessage());
        }
        return null;
    }

先谢谢了。

推荐答案

您需要使用

Multipartbody.Builder()

而不是

MultipartBuilder

其工作

这篇关于使用okHttp图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆