Problem when uploading images from android to my server localhost

0

I am programming to upload my images to my server in a folder but it does not upload and I get the following error:

  

org.json.JSONException: Value [] of type org.json.JSONArray can not be converted to JSONObject

The times I tried them (I think about 10), 4 entered by OnResponse and the others by onError and from there it always enters by onError and shows me that error that I mention above. I have not modified any code. And just in case, when I entered by onResponse I would not upload any image to my %%% of my server folder. Thank you very much for your help, friends.

Here is my code of ../fotos/.. that would be the webservice that connects me the android with the database.

  <?php
  $host_name = "localhost";
  $database = "base_olx";
  $user = "root";
   $password = "";

  $con = mysqli_connect($host_name,$user,$password,$database);

  $json = json_decode(file_get_contents("php://input"),true);

 //$nombre = $json["nombre"];
  $nombre = "nombreestatico";
  $listaImagen = $json["listaImagen"];

  $respuesta = array();

  if(isset($listaImagen)){

     if(is_array($listaImagen)){
       foreach($listaImagen as $imagen){
         $decodedImagen = base64_decode($imagen);
         $return = 
            file_put_contents("../fotos/".$nombre."_".$i.".JPG",$decodedImagen);
         $i++;
         $respuesta["ok"] = 1;
       }
     }
     echo  json_encode( $respuesta);
  }else{
     $respuesta["ok"] = 0;
     echo  json_encode( $respuesta);
  }
?>     

and here is my code in android studio:

 public class AgregarMultiplesImagenes extends AppCompatActivity implements 
  View.OnClickListener {                                                           

 private Button btnSelImagenes,btnSubirImagenes;                                                                                                         
 private ArrayList<String> listaDeImagenes;                                                                                                              
 private ArrayList<String> ListaDeNombres;                                                                                                               
 private ArrayList<String> listaDeRutas;                                                                                                                 
 private ProgressDialog progressDialog;                                                                                                                  

@Override                                                                                                                                               
protected void onCreate(Bundle savedInstanceState) {                                                                                                    
super.onCreate(savedInstanceState);                                                                                                                 
setContentView(R.layout.activity_agregar_multiples_imagenes);                                                                                       
InicializarObjetosForm();                                                                                                                           
inicializarOtrosObjetos();                                                                                                                          
}                                                                                                                                                       

  private void inicializarOtrosObjetos() {                                                                                                                

  this.listaDeImagenes = new ArrayList<>();                                                                                                          
  this.listaDeRutas = new ArrayList<>();                                                                                                             
  this.ListaDeNombres = new ArrayList<>();                                                                                                           
  this.progressDialog = new ProgressDialog(this);                                                                                                    
 }                                                                                                                                                       

  private void InicializarObjetosForm() {                                                                                                                 

    //enlazando al layout                                                                                                                              
    this.btnSelImagenes = (Button) 
    findViewById(R.id.btnSeleccionarImagenes);                                                                           
    this.btnSubirImagenes = (Button) findViewById(R.id.btnSubirImagenes);                                                                               

   //Poniendo eventoss                                                                                                                                 

     this.btnSelImagenes.setOnClickListener(this);                                                                                                       
     this.btnSubirImagenes.setOnClickListener(this);                                                                                                     
    }                                                                                                                                                       

  @Override                                                                                                                                               
  public void onClick(View v) {                                                                                                                           

 switch (v.getId()) {                                                                                                                               
     case R.id.btnSeleccionarImagenes:                                                                                                              

         //llamando con un intent al seleccionador de imagenes                                                                                      
         Intent abrirElChooserImagenes = new Intent(this, 
  PickImageActivity.class);                                                                 

  abrirElChooserImagenes.putExtra(PickImageActivity.KEY_LIMIT_MAX_IMAGE, 3);                                                                 

  abrirElChooserImagenes.putExtra(PickImageActivity.KEY_LIMIT_MIN_IMAGE, 3);                                                                 
         startActivityForResult(abrirElChooserImagenes, 
  PickImageActivity.PICKER_REQUEST_CODE);                                                     

         break;                                                                                                                                     
       case R.id.btnSubirImagenes:                                                                                                                    
          this.progressDialog.setMessage("Subiendo imagenes....");                                                                                  
          this.progressDialog.show();                                                                                                               

         subirImagenes();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
         break;                                                                                                                                     
 }                                                                                                                                                  
}                                                                                                                                                       

 private void subirImagenes(){                                                                                                                           

 if(this.listaDeImagenes.isEmpty()){                                                                                                                 
                Toast.makeText(this,"Debe seleccionar 3 
   imagenes",Toast.LENGTH_LONG).show();                                                        
                return;                                                                                                                             

 }                                                                                                                                                   

  JsonArray jsonArray = new JsonArray();// el array donde iran todas las 
  imagenes codificadas en Base64                                               
  for(String imagenEncode: this.listaDeImagenes){                                                                                                     
        jsonArray.add(imagenEncode);                                                                                                                
 }                                                                                                                                                   

  JSONObject jsonObject = new JSONObject();//Aqui van a ir los parametros 
 que se le van a mandar como POST                                            

  try {                                                                                                                                               
    jsonObject.put("listaImagen",jsonArray);                                                                                                        
  } catch (JSONException e) {                                                                                                                         
    e.printStackTrace();                                                                                                                            
  }                                                                                                                                                   
        String url = 
       "http://"+MainActivity.IP+"/Olx/WSProys1/SubirMuchasImagenes.php";                                                             
       JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, 
       jsonObject, new Response.Listener<JSONObject>() {                                  
    @Override                                                                                                                                       
    public void onResponse(JSONObject response) {                                                                                                   
        try {                                                                                                                                




     Toast.makeText(getApplicationContext(),response.get("ok")
    .toString(),Toast.LENGTH_LONG).show();                                         
        } catch (JSONException e) {                                                                                                                 
            e.printStackTrace();                                                                                                                    

        }                                                                                                                                           
    }                                                                                                                                               
  }, new Response.ErrorListener() {                                                                                                                   
    @Override                                                                                                                                       
    public void onErrorResponse(VolleyError error) {                                                                                                
                  Toast.makeText(getApplicationContext(),error.getMessage()
    ,Toast.LENGTH_LONG).show();                                              
      }                                                                                                                                               
   })      ;                                                                                                                                           

  Volley.newRequestQueue(this).add(jsonObjectRequest);                                                                                                

  this.progressDialog.dismiss();                                                                                                                      

}                                                                                                                                                       

 @Override                                                                                                                                               
  protected void onActivityResult(int requestCode, int resultCode, @Nullable 
  Intent data) {                                                               
 super.onActivityResult(requestCode, resultCode, data);                                                                                              

  if(requestCode == PickImageActivity.PICKER_REQUEST_CODE){                                                                                           

    if(resultCode != PickImageActivity.RESULT_OK){                                                                                                  
       return;                                                                                                                                      
    }                                                this.listaDeRutas = 
   data.getStringArrayListExtra(PickImageActivity.KEY_DATA_RESULT);           
    if(this.listaDeRutas != null && !this.listaDeRutas.isEmpty()){                                                                                  
            this.listaDeImagenes.clear();                                                                                                           
            for(int i = 0; i<this.listaDeRutas.size(); i++){                                                                                        
                try {                                                                                                                               
                    Bitmap bitmap = 

MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.fromFile(new 
File(this.listaDeRutas.get(i))));  
                    ByteArrayOutputStream byteArrayOutputStream = new 
ByteArrayOutputStream();                                                      

 bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);                                                          
                    String bitMapCodificado = 
  Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);                            
                    this.listaDeImagenes.add(bitMapCodificado);                                                                                     
                    File laImagen = new File(this.listaDeRutas.get(i));                                                                             
                    this.ListaDeNombres.add(laImagen.getName());                                                                                    

                } catch (IOException e) {                                                                                                           
                    e.printStackTrace();                                                                                                            
                }                                                                                                                                   
            }                                                                                                                                       
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
} 
    
asked by David Garcia 24.10.2018 в 14:57
source

0 answers