我是使用改造2的新手,想将数据发布到服务器 我要发送的信息是模态类,在此模态类中,我有一些字段和一个数据数组列表,并且每个这些数组元素都有一个需要上载到服务器的图像。
这些数组中的图像是否需要多部分处理?
总的来说,我一直在这样做,但是没用吗?
@POST("send_cheques.php")
Call<Example> SaveCheques(@Body AllCheques cheque_payment);
但是有些事情是错误的
这就是我要发送到服务器的内容
{"ClientId":16,
"CityId":1,
"TotalCheque":2,
"Cheques":[
{
"ChequeAmount": 15000,
"ChequeNumber":205,
"ChequeDate": "11/7/18",
"ChequeImage":"www/ass/img10111"
},
{
"ChequeAmount": 21000,
"ChequeNumber":297,
"ChequeDate": "14/7/18",
"ChequeImage":"www/ass/img10111"
}
]}
这是模态类
public class AllCheques {
@SerializedName("ClientId")
@Expose
public Integer clientid;
@SerializedName("CityId")
@Expose
public Integer cityid;
@SerializedName("TotalCheque")
@Expose
public Integer totalcheque;
@SerializedName("Cheques")
@Expose
public ArrayList<Cheque_Payment> cheques;
public AllCheques(Integer cityid , Integer clientid , Integer totalcheque ,ArrayList<cheques> chequesall){
this.cityid = cityid;
this.clientid = clientid;
this.totalcheque =totalcheque;
this.chequesall = chequesall;
}
public Integer getCityId() {
return cityid ;
}
public void setCityId(Integer cityid ) {
this.cityid = cityid;
}
public Integer getClientId() {
return clientid;
}
public void setClientId(Integer clientid) {
this.clientid = clientid;
}
public Integer getTotalcheque() {
return totalcheque ;
}
public void setTotalcheque(Integer totalcheque) {
this.totalcheque = totalcheque;
}
public ArrayList<cheques> chequesall = new ArrayList<>();
public ArrayList<cheques> getChequesall() {
return chequesall;
}
public void setChequesall(ArrayList<cheques> chequesall )
{
this.chequesall = chequesall;
}
public static class cheques{
@SerializedName("ChequeAmount")
@Expose
public Integer chequeAmount;
@SerializedName("ChequeNumber")
@Expose
public Integer chequeNumber;
@SerializedName("ChequeDate")
@Expose
public String chequeDate;
@SerializedName("ChequeImage")
@Expose
public String chequeImage;
public cheques( Integer chequeAmount , Integer chequeNumber ,String chequeDate, String chequeImage)
{
this.chequeAmount = chequeAmount;
this.chequeNumber = chequeNumber;
this.chequeDate = chequeDate;
this.chequeImage = chequeImage;
}
public cheques() {
}
public Integer getChequeAmount() {
return chequeAmount ;
}
public void setChequeAmount(Integer chequeAmount) {
this.chequeAmount =chequeAmount;
}
public Integer getChequeNumber() {
return chequeNumber ;
}
public void setChequeNumber(Integer chequeNumber) {
this.chequeNumber =chequeNumber;
}
public String getChequeDate() {
return chequeDate ;
}
public void setChequeImage(String chequeImage) {
this.chequeImage =chequeImage;
}
public String getChequeImage() {
return chequeImage ;
}
}
}
答案 0 :(得分:0)
例如,使用Retrofit
在服务器上上传图片:
@MultiPart
@POST("send_cheques.php")
Call<Example> SaveCheques(
@Query("clientid") String clientid,
@Query("cityid") String cityid,
@Query("totalcheque") String totalcheque,
@Part MultipartBody.Part AllCheques cheque_payment);
比:
File file = new File(imagePathHere);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("imageName", "imageName.jpeg", requestBody);
现在您可以通过传递SaveCheques
作为参数来调用body
:
RetrofitAPICalls apiCalls = RetrofitAPIClient.getRetrofit().create(RetrofitAPICalls.class);
Call<Example> uploadPhoto = apiCalls.SaveCheques(clientId, cityId, totalcheque, body);
updateProfile.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
if(response.isSuccessful())
//Image uploaded
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
}
});