Search code examples
androidandroid-volleyandroidhttpclient

Android Sending PDF to server using Volley


I wanna send a PDF file stored in internal storage ( InternalStrorage/PDF/OCK.pdf )

I already sent String Data to my php server using Android Volley without problems via this function:

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);

}

I wanna send the PDF file with my data in the same time.


Solution

  • Finally i find the solution. 1- Just convert the PDF to to byte array 2- Byte Array to base64 3- Send it like normal strings

    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);