相关疑难解决方法(0)

Retrofit(2.0 beta2)Multipart文件上传不起作用

我正在使用Square Retrofit版本2.0 beta2.我试过按照本教程.我试图将位图图像上传到服务器,但不知何故代码不起作用.我尝试使用邮递员测试我的服务器,我能够发布照片甚至能够检索它.这是我的烧瓶控制器.

@app.route('/api/photo/user/<int:user_id>', methods=["POST"])
    def post_user_photo(user_id):
        app.logger.info("post_user_photo=> user_id:{}, photo: {}".format(
            user_id,
            request.files['photo'].filename,
        ))
        user = User.query.get_or_404(user_id)
        try:
            user.photo = request.files['photo'].read()
        except Exception as e:
            app.logger.exception(e)
            db.session.rollback()
            raise
        db.session.commit()
        return "", codes.no_content
Run Code Online (Sandbox Code Playgroud)

我用邮递员来测试我的控制器,这是邮递员生成的请求.

POST /api/photo/user/5 HTTP/1.1
Host: blooming-cliffs-9672.herokuapp.com
Cache-Control: no-cache
Postman-Token: 8117fb79-4781-449d-7d22-237c49b53389
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="photo"; filename="sfsu.jpg"
Content-Type: image/jpeg


----WebKitFormBoundary7MA4YWxkTrZu0gW
Run Code Online (Sandbox Code Playgroud)

我已经定义了改造服务和上传图像,这是我的Android代码.接口部分

  @Multipart
    @POST("/api/photo/user/{userId}")
    Call<Void> uploadUserProfilePhoto(@Path("userId") Integer userId, @Part("photo") RequestBody photo);
Run Code Online (Sandbox Code Playgroud)

客户端构建器部分

  public static BeamItService getService(){
        if (service == null) {
            OkHttpClient client = new OkHttpClient(); …
Run Code Online (Sandbox Code Playgroud)

android retrofit okhttp

5
推荐指数
1
解决办法
6718
查看次数

标签 统计

android ×1

okhttp ×1

retrofit ×1