使用Retrofit上传Multipart

时间:2015-05-07 09:58:52

标签: android retrofit

我正在尝试使用Retrofit将文件(音频文件)上传到服务器。 但我得到了

  

失败:retrofit.RetrofitError:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)接受第1行第8行路径上格式错误的JSON $

。虽然之前,我使用异步任务mutipartEntityBuilder并成功使用httpPost发送文件。 这是我正在使用的客户端代码。如果您需要更多信息(服务器端),我会发布它。

谢谢。

  @POST("/UploadImageFile")
    @Multipart
    void upload(@Part("file") TypedFile file,
                @Part("Name") String Name,
                @Part("Taskcode") String Taskcode,
                @Part("ClientId") String ClientId,
                @Part("Type") String Type,
                Callback<String> cb);

 RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(BASE_URL)
                .setClient(new OkClient(new OkHttpClient()))
                .build();

        restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
        RestAPI post = restAdapter.create(RestAPI.class);


        TypedFile typedFile = new TypedFile("multipart/form-data", new File(Path));
        String NameOfFile = typedFile.fileName();
        post.upload(typedFile,NameOfFile, currentTaskCode,HTTPRequestUserAuthentication.AppID,Type,new Callback<String>() {
            @Override
            public void success(String s, Response response) {
                Log.d(TAG,"Success: "+s);
            }

            @Override
            public void failure(RetrofitError error) {
                Log.d(TAG,"failure: "+error.toString());
            }
        });

0 个答案:

没有答案
相关问题