2

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.

Style5000
  • 33
  • 8
  • Try using Multipart Entity. Files can be send using multipart. – Ashish John Sep 07 '17 at 12:08
  • @AshishJohn do you've a great tutorial or a helpful link? – Style5000 Sep 07 '17 at 13:04
  • Or you can follow : 1. Convert pdf file to byte array 2. byte array to Base64 encode byte array byte[] encodedBytes = Base64.encodeBase64(origByteArr) and String base64Str = new String(encodedBytes); Send it using any networking library available at your end – Napolean Sep 07 '17 at 13:28
  • @BenyamineMalki some of the helpful websites I have found are: vogella.com, tutorialspoint.com/android, java2s.com – Ashish John Sep 08 '17 at 06:04

1 Answers1

0

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);
Style5000
  • 33
  • 8