2017-03-01 111 views
0

如何发送带有包含图像文件和其他属性的json主体的Retrofit2.0发布请求 我有一个对象模型,用键和图像序列化字符串来保存图像路径如下Android如何发送包含图像文件和其他属性的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(); 
     } 
    }); 
} 

我只想知道如何使用post对象请求发送图像,我在接口中使用@Body ...我应该在pojo类中使用多部分,还是我该如何做...请帮助我出

任何帮助,将不胜感激

回答

0

你必须使你的形象,以位图,然后串。

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方法。

+0

不要我需要发送它在多部分编码?在将图像转换为位图后,我到底需要做些什么?我是否应该像使用retrofit @body一样以同样的方式发送它? –

+0

......你制作了SeekerProfileModel的另一个属性并将其分配给它。然后发送对象...其余的工作应该由服务器完成。 – sadat

+0

意味着不需要多部分的东西? –

0

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, "[email protected]", "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本教程。

+0

我有23个其他字段以及图像,如fname,lname,电子邮件,地址,状态,城市..等等和一个字段称为profilepic ...所以我一直在序列化pojo模型类接口像多部分 POST( “empowerapp/seekerreg.php”) 调用 registerSeeker( Body SeekerProfileModel profileModel); –

+0

它表示没有使用以上代码找到翻新注释.. @ user7357013 –

相关问题