【问题标题】:Retrofit 2 parsing outgoing dataRetrofit 2 解析传出数据
【发布时间】:2017-02-19 16:09:53
【问题描述】:

你好 Stackoverflow 的伙计们! 这是我的第一篇文章,确实是我对 Retrofit 2 的大问题 我希望有人能帮助解决这个问题。

这是我的接口类:

public interface PicUploadInterface {
@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part MultipartBody.Part filePart);

和 apimanager 类:

public class Apimanager {

public static void upload(String coda, File file) {
    // create upload service client
    final String API_BASE_URL = "http://10.0.3.2/web2Demo/public/";
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    httpClient.addInterceptor(logging);  

    Retrofit builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build();

    PicUploadInterface service = builder.create(PicUploadInterface.class);
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    MultipartBody.Part filePart  = MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);

    String descriptionString = coda;
    String AuthString = "Send picture";
    RequestBody description = RequestBody.create(MediaType.parse("text/plain"), descriptionString);
    RequestBody authorization = RequestBody.create(MediaType.parse("text/plain"), AuthString);

    Call<UserModelResponse> call = service.uploadMultipleFiles(authorization, description, filePart);
    call.enqueue(new Callback<UserModelResponse>() {
        @Override
        public void onResponse(Call<UserModelResponse> call,
                               Response<UserModelResponse> response) {
            Log.v("Upload", "success");
        }
        @Override
        public void onFailure(Call<UserModelResponse> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}

我以这种方式使用代码:

      File file = new File(textViewFile.getText().toString()); // getting this string "/storage/emulated/0/bichooser/1476172212892.jpg"
      connectivity.com.pack.Apimanager.upload(name, file);

最后我的 HttpLoggingInterceptor 显示我无法在我的 restApi 中找到它是什么以及如何解析它

10-11 03:50:12.944 1393-1681/transfer.com.bermou D/pager.tab.component.RegisterFormFragments: onImageChosen: /storage/emulated/0/bichooser/1476172212892.jpg 10-11 03:50:41.236 1393-2102/transfer.com.bermou D/OkHttp: --> POST http://10.0.3.2/web2Demo/public/userhttp/1.1 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/OkHttp:内容类型: 多部分/表单数据;边界=66e09d68-b88d-4ace-b978-a56e1a6ab66b 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/OkHttp: 内容长度:59914 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm:VFY:无法找到签名中引用的类 (Ljava/nio/文件/路径;) 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm:VFY:无法找到课程 在签名中引用 ([Ljava/nio/file/OpenOption;) 10-11 03:50:41.244 1393-2102/transfer.com.bermou I/dalvikvm:找不到 方法 java.nio.file.Files.newOutputStream,引用自方法 okio.Okio.sink 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm:VFY:无法解析静态方法 35770: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream; 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/dalvikvm: VFY: 在 0x000b 10-11 03:50:41.244 处替换操作码 0x71 1393-2102/transfer.com.bermou W/dalvikvm:VFY:无法找到课程 在签名中引用 (Ljava/nio/file/Path;) 10-11 03:50:41.248 1393-2102/transfer.com.bermou W/dalvikvm:VFY:无法找到课程 在签名中引用 ([Ljava/nio/file/OpenOption;) 10-11 03:50:41.248 1393-2102/transfer.com.bermou I/dalvikvm:找不到 方法 java.nio.file.Files.newInputStream,引用自方法 okio.Okio.source 10-11 03:50:41.248 1393-2102/transfer.com.bermou W/dalvikvm: VFY: 无法解析静态方法 35769: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream; 10-11 03:50:41.248 1393-2102/transfer.com.bermou D/dalvikvm: VFY: 在 0x000b 10-11 03:50:41.268 处替换操作码 0x71 1393-2102/transfer.com.bermou D/dalvikvm:GC_FOR_ALLOC 释放 2130K, 30% 免费 7056K/9952K,暂停 4ms,共 4ms 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ������JFIF������������������C�� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: �����������}��!1AQa"q2���#B��R��$3br� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������� ������������������������������������������������������
10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ���������w��!1AQaq"2�B���� #3R�br� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������� ��������������������������������������������?��x�PO�J+ �2 年�Ɇ���pVBQv����0\Bs��4� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: �r�I����TӍ�Js�)(������Rq��}l,�q 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ��]�\I�C��C�#%��e�\�2��5�r�㒝H՚�8�e 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: /���i��sn��Vnץ�%(�f�B�S�zr��

我找不到从改造 2.1.0 到我的 restApi 的内容,哪一部分是我的字符串或图像数据? 我确实根据 stackoverflow 提示和另一个站点进行了多次更改,但我的结果几乎相同! (我从拦截器日志中做了短图像二进制输出) 如果有人有想法,请分享。谢谢

【问题讨论】:

  • 你在改造回调中得到什么响应?
  • 确实是我在发布数据时遇到的大问题,而此时响应对我来说并不重要。

标签: android retrofit


【解决方案1】:

您是否尝试过在服务调用中直接使用:

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 

...

Call<UserModelResponse> call = service.uploadMultipleFiles(authorization, description, requestFile);

并且更新这个:

@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part("file\"; filename=\"myFilename\" ") RequestBody filePart);

另外,如果您想查看您对 restApi 的调用,您可以使用 Charles Proxy 作为反向代理并查看您的调用和响应。

【讨论】:

  • 我的问题仍未解决,okhhtp 日志以 Content-Type: multipart/form-data; 开头边界=66e09d68-b88d-4ace-b978-a56e1a6ab66b 并且不包括授权、描述标签和继续(���������������������������� ������������������������������������������������������ ������������) 图像字节码!
猜你喜欢
  • 1970-01-01
  • 2018-12-17
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2019-12-08
相关资源
最近更新 更多