Interface
public interface iUpload{
@Multipart
@POST("/uploadmultiplepropimages/")
SamplePojoClass getUploadData(
@Part("prop_id") RequestBody prop_id,
@Part("type") RequestBody type,
@Part("prop_photos") TypedFile prop_photos
);
}
I'm sending like this. I cant send request body text like this.
@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;
}`
It says error:
Cannot access ByteString class file.
I hope you have added okio
dependency in your gradle file. This will resolve Cannot access ByteString class file error.
compile 'com.squareup.okio:okio:1.13.0'
Then Edit your iUpload
interface file like:
public interface iUpload{
@Multipart
@POST("/uploadmultiplepropimages/")
SamplePojoClass getUploadData(
@Part MultipartBody.Part file
@Part MultipartBody.Part prop_id,
@Part MultipartBody.Part type
);
}
Then write MultipartBody.Part
like this:
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");
and finally pass these parameters to your api like this:
uploadImageResponse = RequestResponse.getUploadData(lFile,id,type);
I hope it will resolve your problem.
Note: Here pFile
is instance of File
. To get file from dicrectory you can write code like:
File pFile = new File("PATH_OF_FILE");
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();
Use the following fuction for your problem
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();
}