首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修改2解析传出数据

修改2解析传出数据
EN

Stack Overflow用户
提问于 2016-10-11 06:50:29
回答 1查看 152关注 0票数 0

你好斯塔克沃夫的人!这是我的第一篇文章,也是我在Retrofit 2上的大问题,我希望一些人能帮我解决这个问题。

这是我的界面课:

代码语言:javascript
运行
复制
public interface PicUploadInterface {
@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part MultipartBody.Part filePart);

以及apimanager课程:

代码语言:javascript
运行
复制
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());
        }
    });
}

我以这样的方式使用代码:

代码语言:javascript
运行
复制
      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/trans.com.bermou D/pager.tab.component.RegisterFormFragments: onImageChosen: onImageChosen 10-11 03:50:41.236 1393-2102/ http://10.0.3.2/web2Demo/public/user http/1.1 10-1103:50:41.244 1393-2102/http://10.0.3.2/web2Demo/public/user:41.244 1393-2102/http://10.0.3.2/web2Demo/public/user边界=66e09d68-b88d-4ace-B 978-a56e1a6ab66b 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/OkHttp:内容-长度: 59914 10-1103:50:41.244 1393-2102/Trans.com.bermou W/dalvikvm: VFY:找不到签名中引用的类(Ljava/nio/file/Path) 10-11 03:50:41.244 1393-2102/trans.com.bermou W/dalvikvm: VFY:找不到签名中引用的类([Ljava/nio/file/Path];) 10-11 03:50:41.244 1393-2102/Trans.com.bermou I/dalvikvm:未能找到方法java.nio.file.Files.newOutputStream,引用方法okio.Okio.sink 10-11 03:50:41.244 1393-2102/Trans.com.bermou W/dalvikvm: VFY:无法解析静态方法35770: Ljava/nio/file/Files;..newOutputStream10-11 03:50:41.244 1393-2102/trans.com.bermou D/dalvikvm: VFY:替换0x000b 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm: VFY:找不到签名中引用的类(Ljava/nio/file/Path) 10-11 03:50:41.248 1393-2102/trans.com.bermou W/dalvikvm: VFY:无法找到签名中引用的类([Ljava/nio/file/OpenOption;) 10-11 03:50:41.248 1393-2102/trans.com.bermou I/dalvikvm:未能找到方法java.nio.file.Files.newInputStream,引用方法okio.Okio.source 10-11 03:50:41.248 1393-2102/trans.com.bermou W/dalvikvm: VFY:无法解析静态方法35769: Ljava/nio/file/Files;..newInputStream10-11 03:50:41.248 1393-2102/trans.com.bermou D/dalvikvm: VFY:替换操作码0x71在0x000b 10-11 03:50:41.268 1393-2102/trans.com.bermou D/dalvikvm: GC_FOR_ALLOC freed 2130 K,30%空闲7056K/9952K,暂停4ms,总计4ms 10-11 03:50:41.272 1393-2102/trans.com.bermou D/OkHttp:������JFIF����������������C��10-11 03:50:41.272 1393-2102/trans.com.bermou D/OkHttp:�����������}��!1AQa“����������������10-11 03:50:41.2721393-2102/Transfer.com.bermouD/OkHttp:%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������������������������� 10-11 03:50:41.272 1393-2102/trans.com.bermou D/OkHttp:���������w��!1�“2�B����#3R�br�10-11 03:50:41.272 1393-2102/trans.com.bermou D/OkHttp:���������w��!1 1AQaq”2�B����#3R�br�10-11 03:50:41.272 1393-2102/trans.com.bermou D/OkHttp:���������w��!���������������������������������������������������?��x�PO�J+�2yr�Ɇ���pVBQv����0\Bs��4�10-11 03:50:41.272 1393-2102/trans.com.bermouD/OkHttp:�r�I����TӍ�Js�)(���߻Rq��}l=================================================================================================================== Q 10-11 03:50:41.272 1393-2102/ /���i��sn��Vnץ�%(�f�B�S�zr��<�Fu��s�W END POST (59914字节体))

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

EN

回答 1

Stack Overflow用户

发布于 2016-10-11 14:41:10

您是否曾尝试直接在服务呼叫中使用:

代码语言:javascript
运行
复制
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 

..。

代码语言:javascript
运行
复制
Call<UserModelResponse> call = service.uploadMultipleFiles(authorization, description, requestFile);

并更新如下:

代码语言:javascript
运行
复制
@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part("file\"; filename=\"myFilename\" ") RequestBody filePart);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39971801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档