[英]Android How to send Retrofit2.0 post request with json body containing imagefiles and other attributes
如何使用包含圖像文件和其他屬性的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;
}
我的界面如下
@Multipart
@POST("empowerapp/seekerreg.php")
Call<ResponseModel> registerSeeker(
@Body SeekerProfileModel profileModel);
我的調用方法如下
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();
}
});
}
我只想知道如何使用后對象請求在接口中使用@Body的情況下發送圖像...我應該在pojo類中使用multipart還是我到底該怎么做..請幫幫我
任何幫助將非常感激
您必須先將圖像轉換為位圖,然后轉換為字符串。
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());
}
然后發送到您的POST方法。
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
// 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) {
}
});
有關更多詳細信息, 請遵循本教程https://learnpainless.com/android/retrofit2/send-multiple-files-to-server-using-retrofi2 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.