首页 > Retrofit okHttp 上传图片总是不行

Retrofit okHttp 上传图片总是不行

这是Retrofit接口代码

Retrofit 调用代码

Retrofit怎么弄都不行 不能成功,但是我用 ( useLibrary 'org.apache.http.legacy')的httpclient可以上传,以下是调用方法:

这是php服务器端接收图片代码


如果你用的是Retrofit2.0 那 Part注解后面 后面应该是MultipartBody.Part 而不是RequestBody


我是这样写的 @Part("image\"; filename=\"image.png\" ") RequestBody image


我也是用httpclient可以成功,但是使用retrofit还要传递token,始终没有成功


方法注解只使用POST,参数注解也只使用BODY,以及单独构造MultipartBody进行上传。
retrofit2.0对上传文件支持并不那么简洁,应该去熟悉okhttp


http://m.blog.csdn.net/article/details?id=51511493 这是使用okhttp的后台是php接收


  Map<String, RequestBody> photoRequestBodyMap = new HashMap<>();


        photoRequestBodyMap.put("Content", RequestBody.create(MediaType.parse("text/plain"), et_body.getText().toString()));

        for (int i = 0; i < picList.size(); i++) {
        
                File file = new File(picList.get(i));
                RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
                
                String key = "image\";filename=\"" + file.getName();
                photoRequestBodyMap.put(key, requestBody);
                
            } catch (Exception e) {
            }
        }


        netApiInterface
                .postMoment(photoRequestBodyMap)

我是这样的。

关于图片上传时要用的key,网上有多种说法。
我这边,实测 String key = "image";filename="" + file.getName();可行

【热门文章】
【热门文章】