2017-09-07 5 views
0

Je veux envoyer un fichier PDF stocké dans la mémoire interne (InternalStrorage/PDF/OCK.pdf)Android Envoi PDF au serveur en utilisant Volley

J'ai déjà envoyé Chaîne de données à mon serveur PHP en utilisant Volley Android sans problème via cette fonction :

private void upload(){ 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, UploadURL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonObject = new JSONObject(response); 
         String Response = jsonObject.getString("response"); 
         Toast.makeText(getApplicationContext(),Response, Toast.LENGTH_LONG).show(); 
         selectedimage.setImageResource(0); 
         selectedimage.setVisibility(View.GONE); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    } 
    ){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> params = new HashMap<>(); 


      params.put("date",DateFormat.getDateTimeInstance().format(new Date()).toString()); 
          params.put("description",editTextDescription.getText().toString()); 
      params.put("statut", whatstatutis()); 
      params.put("action",editTextImediatActionTaken.getText().toString()); 
      return params; 
     } 



    }; 
    MySingleton.getInstance(getApplicationContext()).addTorequesteque(stringRequest); 

} 

Je veux envoyer le fichier PDF avec mes données dans le même temps.

+0

Essayez multipart entité. Les fichiers peuvent être envoyés en utilisant multipart. –

+0

@AshishJohn avez-vous un excellent tutoriel ou un lien utile? –

+0

Ou vous pouvez suivre: 1. Convertir un fichier pdf à tableau d'octets 2. tableau d'octets à un tableau base64 encode octets octet [] encodedBytes = Base64.encodeBase64 (origByteArr) et Chaîne base64Str = new String (encodedBytes); Envoyez-le en utilisant une bibliothèque de mise en réseau disponible à votre bout – Napolean

Répondre

0

Enfin, je trouve la solution. 1- Il suffit de convertir le fichier PDF au tableau d'octets 2 Tableau d'octets à base64 3- Envoyer comme des chaînes normales

byte[] data = null; 
    File filessss = new File(Link_Of_The_PDF.pdf); 

    try { 

     data = FileUtils.readFileToByteArray(filessss); 

    } catch (IOException e) { 

     e.printStackTrace(); 

    } 

    return Base64.encodeToString(data, Base64.DEFAULT);