你好斯塔克沃夫的人!这是我的第一篇文章,也是我在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/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的内容,我的字符串或图像数据是哪一部分?我确实根据堆栈溢出提示和另一个站点更改了很多次,但是我的结果几乎是一样的!(我做了截取日志中的短图像二进制输出),如果有人有想法,请分享。谢谢
发布于 2016-10-11 14:41:10
您是否曾尝试直接在服务呼叫中使用:
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作为反向代理,并查看您的调用和响应。
https://stackoverflow.com/questions/39971801
复制相似问题