接口
public interface iUpload{
@Multipart
@POST("/uploadmultiplepropimages/")
SamplePojoClass getUploadData(
@Part("prop_id") RequestBody prop_id,
@Part("type") RequestBody type,
@Part("prop_photos") TypedFile prop_photos
);
}
我是这样发送的。我不能像这样发送请求正文。
@Override
protected Void doInBackground(String... params) {
String s = params[0];
File photoFile = new File(s);
System.out.println("file path:"+photoFile);
TypedFile photoTypedFile = new TypedFile("image/png", photoFile);
RequestBody idd = RequestBody.create(MediaType.parse("text/plain"), "");
RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "single");
try {
//uploadImageResponse = RequestResponse.getUploadData(AccountUtils.getProfileId(),photoTypedFile);
uploadImageResponse = RequestResponse.getUploadData(idd,type,photoTypedFile);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}`
它说错误:
无法访问ByteString类文件。
答案 0 :(得分:3)
我希望您在gradle文件中添加了okio
依赖项。这将解决无法访问ByteString类文件错误。
compile 'com.squareup.okio:okio:1.13.0'
然后编辑您的iUpload
接口文件,如:
public interface iUpload{
@Multipart
@POST("/uploadmultiplepropimages/")
SamplePojoClass getUploadData(
@Part MultipartBody.Part file
@Part MultipartBody.Part prop_id,
@Part MultipartBody.Part type
);
}
然后像这样写MultipartBody.Part
:
RequestBody lRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), pFile);
MultipartBody.Part lFile = MultipartBody.Part.createFormData("file", pFile.getName(), lRequestBody);
MultipartBody.Part id = MultipartBody.Part.createFormData("prop_id", "WRITE_ID_HERE");
MultipartBody.Part type = MultipartBody.Part.createFormData("type", "WRITE TYPE HERE");
最后将这些参数传递给你的api:
uploadImageResponse = RequestResponse.getUploadData(lFile,id,type);
我希望它能解决你的问题。
注意:此处pFile
是File
的实例。要从dicrectory获取文件,您可以编写如下代码:
File pFile = new File("PATH_OF_FILE");
答案 1 :(得分:0)
I have done Multipart upload in okhttp. I hope this will help.
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
multipartBuilder.addFormDataPart("prop_photos", photoFile, RequestBody.create(MEDIA_TYPE_PNG, file));
multipartBuilder
.addFormDataPart("type", type)
.addFormDataPart("prop_id", prop_id);
RequestBody requestBody = multipartBuilder.build();
Request request1 = new Request.Builder().url(urlString).post(requestBody).build();
答案 2 :(得分:-1)
使用以下功能解决您的问题
public static RegisterResponse Uploadimage(String id, String photo,String proof) {
File file1 = null, file2 = null;
try {
if (photo.trim() != null && !photo.trim().equals("")) {
file1 = new File(photo);
}
if (proof.trim() != null && !proof.trim().equals("")) {
file2 = new File(proof);
}
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(WEB_SERVICE + "protoupload.php?");
MultipartEntity reqEntity = new MultipartEntity();
// reqEntity.addPart("studentid", new StringBody(
// Global.profileDetail.id));
reqEntity.addPart("id", new StringBody(id));
if (file1 == null) {
reqEntity.addPart("photo", new StringBody(""));
} else {
FileBody bin1 = new FileBody(file1);
reqEntity.addPart("photo", bin1);
}
if (file2 == null) {
reqEntity.addPart("proof", new StringBody(""));
} else {
FileBody bin2 = new FileBody(file2);
reqEntity.addPart("proof", bin2);
}
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
String inputStreamString = EntityUtils.toString(resEntity);
if (inputStreamString.contains("result")) {
return new Gson().fromJson(inputStreamString,
RegisterResponse.class);
}
} catch (Exception ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
return new RegisterResponse();
}