title: Image upload error using api in android studio in java language . not able to upload image
body: <p>I'm trying to upload image to xampp server, with android-studio and php But I will not succeed Please help me</p>
  <p>here is my code(java and php):</p>
  <p>java code: in this codes im trying to select picture from gallery and show a dialog and then uploading image...</p>
  <p><strong>signup.java</strong></p>
  <pre><code>public class Sign_up extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
      
          ApiInterface apiInterface;
          Intent myfileintent;
          Spinner spinner,spinner1;// one for course,and another is indurestry types.
          String filepath=&quot; &quot;;
      
      
          //adding spinner list here.
          String[] Course = {&quot;PHP&quot;,&quot;WORDPRESS&quot;,&quot;ANDROID&quot;,&quot;FLUTEER&quot;,&quot;PYTHON&quot;,&quot;DIGITAL MARKETING&quot;};
          String[] I_types = {&quot;15 DAYS&quot;,&quot;INDUSTRIAL VISIT&quot;};
          EditText R_name,R_email,R_phone,R_password,R_cpassword;
          TextView c_pp,c_ab,c_af,c_mm,c_pay,R_af,R_ab,R_mark,R_payment,R_passphoto;
      
      
          @Override
          protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
              switch (requestCode){
      
                  case 10:
                      if (resultCode==RESULT_OK){
                          String path=data.getData().getPath();
                          R_passphoto.setText(path);
                         // uploadImage();
                      }
                      break;
      
                  case 20:
                      if (resultCode==RESULT_OK){
                          String path=data.getData().getPath();
                          R_af.setText(path);
                         // uploadImage();
                      }
                      break;
      
                  case 30:
                      if (resultCode==RESULT_OK){
                          String path =data.getData().getPath();
                          R_ab.setText(path);
                          //uploadImage();
                      }
                      break;
      
                  case 40 :
                      if (resultCode==RESULT_OK){
                          String path =data.getData().getPath();
                          R_mark.setText(path);
                          //uploadImage();
                      }
                      break;
                  case 50:
                      if (resultCode==RESULT_OK){
                          String path =data.getData().getPath();
                          R_payment.setText(path);
                         // uploadImage();
                      }
                      break;
                  default:
                      Toast.makeText(Sign_up.this,&quot;chosse file correctly&quot;,Toast.LENGTH_LONG).show();
      
              }
          }
      
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_sign_up);
              apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
      
              //spinner hookup is here.
              spinner=findViewById(R.id.course_spinner);
              spinner1=findViewById(R.id.spinner4);
      
              //all edittext hookup is here..
              R_name=findViewById(R.id.name);
              R_email=findViewById(R.id.email);
              R_phone=findViewById(R.id.phone);
              R_passphoto=findViewById(R.id.editText2);
              R_af=findViewById(R.id.editTex3);
              R_ab=findViewById(R.id.editTex);
              R_mark=findViewById(R.id.editTex2);
              R_payment=findViewById(R.id.editTex4);
              R_password=findViewById(R.id.pwd);
              R_cpassword=findViewById(R.id.cpwd);
      
              //choose file hookup is here
              c_pp=findViewById(R.id.textView);
              c_af=findViewById(R.id.textView1);
              c_ab=findViewById(R.id.textView2);
              c_mm=findViewById(R.id.textView3);
              c_pay=findViewById(R.id.textView5);
      
              //adding first spinner list
              ArrayAdapter&lt;String&gt; aa = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item, Course);
              aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              //first spinner working
              spinner.setAdapter(aa);
              spinner.setOnItemSelectedListener(this);
      
              //adding second spinner list
              ArrayAdapter&lt;String&gt; bb = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item, I_types);
              bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              spinner1.setAdapter(bb);
              spinner1.setOnItemSelectedListener(this);
      
              // all file choose control here..
              c_pp.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                      myfileintent.setType(&quot;*/*&quot;);
                      startActivityForResult(myfileintent,10);
                  }
              });
              c_af.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                      myfileintent.setType(&quot;*/*&quot;);
                      startActivityForResult(myfileintent,20);
                  }
              });
              c_ab.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                      myfileintent.setType(&quot;*/*&quot;);
                      startActivityForResult(myfileintent,30);
                  }
              });
              c_mm.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                      myfileintent.setType(&quot;*/*&quot;);
                      startActivityForResult(myfileintent,40);
                  }
              });
              c_pay.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                      myfileintent.setType(&quot;*/*&quot;);
                      startActivityForResult(myfileintent,50);
                  }
              });
      
      
      
          }
      
          public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int position, long id ){
              Spinner spin = (Spinner)parent;
              Spinner spin2 = (Spinner)parent;
              if(spin.getId() == R.id.course_spinner)
              {
      
                  Toast.makeText(this, &quot;Your choose :&quot; + Course[position],Toast.LENGTH_SHORT).show();
              }
              if(spin2.getId() == R.id.spinner4)
              {
                  Toast.makeText(this, &quot;Your choose :&quot; + I_types[position],Toast.LENGTH_SHORT).show();
              }
      
          }
      
          public void onNothingSelected(AdapterView&lt;?&gt; parent){
              //Toast.makeText(this, &quot;Choose Countries :&quot;, Toast.LENGTH_SHORT).show();
          }
      
      
      
          @Override
          public void onPointerCaptureChanged(boolean hasCapture) {
      
          }
      
      //register working model
          public void registeruser(View view)
          {
              Call&lt;responsemodel&gt; responsemodelCall=apiInterface.registeruser(R_name.getText().toString(),R_email.getText().toString(),R_phone.getText().toString(),spinner.getSelectedItem().getClass().toString(),spinner1.getSelectedItem().getClass().toString(),R_password.getText().toString());
              responsemodelCall.enqueue(new Callback&lt;responsemodel&gt;() {
                  @Override
                  public void onResponse(Call&lt;responsemodel&gt; call, Response&lt;responsemodel&gt; response) {
                      if (response.body()!=null)
                      {
                          responsemodel responsemodel= response.body();
                          Log.i(&quot;Response&quot;, String.valueOf(responsemodel.isresponse_code()));
                          if(responsemodel.getStatus().equals(&quot;success&quot;)){
                              SharedPreferences sp=getSharedPreferences(&quot;credentials&quot;,MODE_PRIVATE);
                              SharedPreferences.Editor editor=sp.edit();
                              editor.putString(&quot;fname&quot;,R_name.getText().toString());
                              editor.putString(&quot;email&quot;,R_email.getText().toString());
                              editor.putString(&quot;number&quot;,R_phone.getText().toString());
                              editor.putString(&quot;cname&quot;,spinner.getSelectedItem().toString());
                              editor.putString(&quot;intern&quot;,spinner1.getSelectedItem().toString());
                              editor.commit();
                              editor.apply();
                              uploadImage();
                              Toast.makeText(Sign_up.this,&quot;user register successfully&quot;,Toast.LENGTH_LONG).show();
      
      
                          }
                          else
                          {
                              Toast.makeText(Sign_up.this,&quot;user not register&quot;,Toast.LENGTH_LONG).show();
                          }
                      }
                  }
      
                  @Override
                  public void onFailure(Call&lt;responsemodel&gt; call, Throwable t) {
      
                      Toast.makeText(Sign_up.this,&quot;something went wrong&quot;,Toast.LENGTH_LONG).show();
      
      
                  }
              });
          }
      
          private void uploadImage(){
              File file=new File(filepath);
              File file_ab=new File(filepath);
              File file_m=new File(filepath);
              File file_p=new File(filepath);
              File file_pa=new File(filepath);
              RequestBody requestBody= RequestBody.create(MediaType.parse(&quot;*/*&quot;), file);
              RequestBody requestBody1= RequestBody.create(MediaType.parse(&quot;*/*&quot;), file_ab);
              RequestBody requestBody2= RequestBody.create(MediaType.parse(&quot;*/*&quot;), file_m);
              RequestBody requestBody3= RequestBody.create(MediaType.parse(&quot;*/*&quot;), file_p);
              RequestBody requestBody4= RequestBody.create(MediaType.parse(&quot;*/*&quot;), file_pa);
              MultipartBody.Part part= MultipartBody.Part.createFormData(&quot;filename&quot;,file.getName(),requestBody);
              MultipartBody.Part part1= MultipartBody.Part.createFormData(&quot;filename1&quot;,file_ab.getName(),requestBody1);
              MultipartBody.Part part2= MultipartBody.Part.createFormData(&quot;filename2&quot;,file_m.getName(),requestBody2);
              MultipartBody.Part part3= MultipartBody.Part.createFormData(&quot;filename3&quot;,file_p.getName(),requestBody3);
              MultipartBody.Part part4= MultipartBody.Part.createFormData(&quot;filename4&quot;,file_pa.getName(),requestBody4);
              Retrofit retrofit=ApiClient.getApiClient();
              ApiInterface apiInterface=retrofit.create(ApiInterface.class);
              Call&lt;responsemodel&gt; responsemodelCall=apiInterface.uploadimage(R_af.getText().toString(),R_ab.getText().toString(),R_mark.getText().toString(),R_payment.getText().toString(),R_passphoto.getText().toString(),part,part1,part2,part3,part4);
              responsemodelCall.enqueue(new Callback&lt;responsemodel&gt;() {
                  @Override
                  public void onResponse(Call&lt;responsemodel&gt; call, Response&lt;responsemodel&gt; response) {
                      if (response.body()!=null)
                      {
                          responsemodel responsemodel= response.body();
                          Log.i(&quot;Response&quot;, String.valueOf(responsemodel.isresponse_code()));
                          if(responsemodel.getStatus().equals(&quot;success&quot;)){
      
                              SharedPreferences sp=getSharedPreferences(&quot;credentials&quot;,MODE_PRIVATE);
                              SharedPreferences.Editor editor=sp.edit();
                              editor.putString(&quot;adharfront&quot;,R_af.getText().toString());
                              editor.putString(&quot;adharback&quot;,R_ab.getText().toString());
                              editor.putString(&quot;mark&quot;,R_mark.getText().toString());
                              editor.putString(&quot;pay&quot;,R_payment.getText().toString());
                              editor.putString(&quot;pass&quot;,R_passphoto.getText().toString());
                              editor.commit();
                              editor.apply();
      
                          }
                      }
                  }
                  @Override
                  public void onFailure(Call&lt;responsemodel&gt; call, Throwable t) {
      
                  }
              });
          }
      }
  </code></pre>
  <p><strong>apiinterface.java</strong></p>
  <pre><code>@FormUrlEncoded
          @POST(&quot;api/register&quot;)
          //agar part nahi work kar raha hai to field change kar dena hai..
          Call&lt;responsemodel&gt; registeruser(@Field(&quot;Name&quot;) String name, @Field(&quot;email&quot;) String email, @Field(&quot;phone&quot;) String phone,
                                           @Field(&quot;cname&quot;) String cname,
                                           @Field(&quot;intern&quot;) String intern, @Field(&quot;password&quot;) String password
          );
      
          @Multipart
          @POST(&quot;api/register&quot;)
          Call&lt;responsemodel&gt;uploadimage(@Part(&quot;adharfront&quot;)String adharfront, String part, @Part(&quot;adharback&quot;)String adharback, String part1, @Part(&quot;mark&quot;)String mark, MultipartBody.Part part2, @Part(&quot;pay&quot;) MultipartBody.Part pay, MultipartBody.Part part3, @Part(&quot;pass&quot;) MultipartBody.Part pass, MultipartBody.Part part4);
  </code></pre>
  <p><strong>responsemodel.java</strong></p>
  <pre><code>public class  register{
          private String fname;
          private String pass;
          private String adharfront;
          private String adharback;
          private String mark;
          private String pay;
          private String email;
          private String password;
          private String number;
          private String cname;
          private String intern;
          private String date;
  
          public String getFname(){
              return fname;
          }
  
          public void setFname(String fname){
              this.fname=fname;
          }
  
          public String getPass(){
              return pass;
          }
  
          public void setPass(String pass){
              this.pass=pass;
          }
  
          public String getAdharfront(){
              return adharfront;
          }
  
          public void setAdharfront(String adharfront){
              this.adharfront=adharfront;
          }
  
          public String getAdharback(){
              return adharback;
          }
  
          public void setAdharback(String adharback){
              this.adharback=adharback;
          }
  
          public String getMark(){
              return mark;
          }
  
          public void setMark(String mark){
              this.mark=mark;
          }
  
          public String getPay(){
              return pay;
          }
  
          public void setPay(String pay){
              this.pay=pay;
          }
  
          public String getEmail(){
              return email;
          }
  
          public void setEmail(String email){
              this.email=email;
          }
  
          public String getPassword(){
              return password;
          }
  
          public void setPassword(String password){
              this.password=password;
          }
  
          public String getNumber(){
              return number;
          }
  
          public void setNumber(String number){
              this.number=number;
          }
  
          public String getCname(){
              return cname;
          }
  
          public void setCname(String cname){
              this.cname=cname;
          }
  
          public String getIntern(){
              return intern;
          }
  
          public void setIntern(String intern){
              this.intern=intern;
          }
  
          public String getDate(){
              return date;
          }
  
          public void setDate(String date){
              this.date=date;
          }
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/0hGLm.png" rel="nofollow noreferrer">error in logcat</a></p>