Android 使用改装发送多部分数据
接口Android 使用改装发送多部分数据,android,retrofit,retrofit2,multipart,Android,Retrofit,Retrofit2,Multipart,接口 public interface iUpload{ @Multipart @POST("/uploadmultiplepropimages/") SamplePojoClass getUploadData( @Part("prop_id") RequestBody prop_id, @Part("type") RequestBody type, @Part("prop_
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类文件
使用以下功能解决您的问题
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();
}
我希望您在gradle文件中添加了
okio
依赖项。这将解决无法通过TestRing类文件访问的错误
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");
uploadImageResponse = RequestResponse.getUploadData(lFile,id,type);
最后将这些参数传递给api,如下所示:
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");
uploadImageResponse = RequestResponse.getUploadData(lFile,id,type);
我希望它能解决你的问题
注意:这里的pFile
是文件的实例
。要从目录中获取文件,可以编写如下代码:
File pFile = new File("PATH_OF_FILE");
不工作时给出以下错误{“headers”:{“namesAndValues”:[“内容处置”,“表单数据;name\u003d\“prop_id\”“]”}}{“headers”:{“namesAndValues”:[“内容处置”,“表单数据;name\u003d\“type\”“]”}@LaxmanMarth请发布您得到的整个错误日志。并将其更新为您的问题。