[英]Android How to send Retrofit2.0 post request with json body containing imagefiles and other attributes
How to send Retrofit2.0 post request with json body containing imagefiles and other attributes I have an object model which is serialized with keys and also image string to hold image paths as below 如何使用包含图像文件和其他属性的json主体发送Retrofit2.0发布请求,我有一个对象模型,该模型通过键和图像字符串进行序列化以保存图像路径,如下所示
public class SeekerProfileModel implements Serializable
{
@SerializedName("fname")
private String fname=null;
@SerializedName("lname")
private String lname=null;
@SerializedName("paswd")
private String password=null;
@SerializedName("promoter")
private String promoter=null;
@SerializedName("dob")
private String dob=null;
@SerializedName("gender")
private String gender=null;
@SerializedName("ph")
private String phone=null;
@SerializedName("email")
private String email=null;
@SerializedName("weight")
private String weight=null;
@SerializedName("height")
private String height=null;
@SerializedName("qualification")
private String qualification=null;
@SerializedName("color")
private String color=null;
@SerializedName("lang_known")
private String languages=null;
@SerializedName("experience")
private String experience=null;
@SerializedName("exp_type")
private String experienceType=null;
@SerializedName("dres_code")
private String dressCode=null;
@SerializedName("vehicle_mode")
private String vehicleMode=null;
@SerializedName("id_proof")
private String idproof=null;
@SerializedName("size")
private String size=null;
@SerializedName("photo1")
private String photo1=null;
@SerializedName("photo2")
private String photo2=null;
@SerializedName("photo3")
private String photo3=null;
@SerializedName("address")
private String address=null;
@SerializedName("landmark")
private String landmark=null;
@SerializedName("location")
private String location=null;
@SerializedName("city")
private String city=null;
@SerializedName("state")
private String state=null;
@SerializedName("contry")
private String contry=null;
@SerializedName("pincode")
private String pincode=null;
@SerializedName("bank_name")
private String bankName=null;
@SerializedName("ac_name")
private String accountName=null;
@SerializedName("ac_number")
private String accontNumber=null;
@SerializedName("ifsc")
private String Ifsc=null;
@SerializedName("br_name")
private String branchName=null;
}
My Interface as below 我的界面如下
@Multipart
@POST("empowerapp/seekerreg.php")
Call<ResponseModel> registerSeeker(
@Body SeekerProfileModel profileModel);
And My method to call is as below 我的调用方法如下
public void registerSeeker(SeekerProfileModel profileModel) {
System.out.println("###exp" + s_exp.getText().toString());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Allconstants.MAIN_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitInterface service = retrofit.create(RetrofitInterface.class);
Call<ResponseModel> call = service.registerSeeker(profileModel);
call.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Response<ResponseModel> response, Retrofit retrofit) {
System.out.println("###coming" + response.body().getStatus());
pd.dismiss();
if (response.body().getStatus().equalsIgnoreCase("Success"))
{
pd.dismiss();
loginSession.createLoginSession(Allconstants.SEEKER,Allconstants.S_REG_ACTIVITY,response.body().getName(), response.body().getId());
Toast.makeText(Registration.this,"successfully registered",Toast.LENGTH_LONG).show();
System.out.println("###coming"+response.body().toString());
}else{
pd.dismiss();
Toast.makeText(Registration.this,"oops!!!something went wrong..try again",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Throwable t) {
pd.dismiss();
Toast.makeText(Registration.this, t.getStackTrace().toString(), Toast.LENGTH_LONG).show();
System.out.println("###error1" + t.getMessage());
System.out.println("###stack trace: ");
t.printStackTrace();
}
});
}
I just want to know how do i send image using post object request where i am using @Body in interface...should i use multipart inside my pojo class or how exactly i have to do it .. please help me out 我只想知道如何使用后对象请求在接口中使用@Body的情况下发送图像...我应该在pojo类中使用multipart还是我到底该怎么做..请帮帮我
Any Help would be much appreciated 任何帮助将非常感激
You have to make your image to bitmap and then string. 您必须先将图像转换为位图,然后转换为字符串。
String base64String = "";
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
image.recycle();
bytes.close();
base64String = Base64.encodeToString(bytes.toByteArray(), Base64.NO_WRAP);
} catch (Exception e) {
OSSUtils.logError(e.getMessage());
}
and then send to your POST method. 然后发送到您的POST方法。
interface.java interface.java
@Multipart
@POST("file_upload/fileUpload.php")
Call<ResponsePojo> submitData(@Part MultipartBody.Part image,
@Part("email") String email,
@Part("website") String website);
}
Mainactivity.java Mainactivity.java
// this will build full path of API url where we want to send data.
Retrofit builder = new Retrofit.Builder().baseUrl(ROOT_URL).addConverterFactory(GsonConverterFactory.create()).build();
SubmitAPI api = builder.create(SubmitAPI.class);
//create file which we want to send to server.
File imageFIle = new File(String.valueOf(realUri));
//request body is used to attach file.
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFIle);
//and request body and file name using multipart.
MultipartBody.Part image = MultipartBody.Part.createFormData("image", imageFIle.getName(),requestBody); //"image" is parameter for photo in API.
Call<ResponsePojo> call = api.submitData(image, "pawneshwergupta@gmail.com", "learnpainless.com"); //we will get our response in call variable.
call.enqueue(new Callback<ResponsePojo>() {
@Override
public void onResponse(Call<ResponsePojo> call, Response<ResponsePojo> response) {
ResponsePojo body = response.body(); //get body from response.
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage(body.getMessage()); //display response in Alert dialog.
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
}
@Override
public void onFailure(Call<ResponsePojo> call, Throwable t) {
}
});
For more details https://learnpainless.com/android/retrofit2/send-multiple-files-to-server-using-retrofi2 follow this tutorial. 有关更多详细信息, 请遵循本教程https://learnpainless.com/android/retrofit2/send-multiple-files-to-server-using-retrofi2 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.