0

I am trying to upload the Base64 encoded string with some other data in the server. Problem is when I passing the form-data without the base64 it's working perfectly. But when I am trying to pass with base64 string its showing error.

My FormActivity.java is :

public void submitForm(){
    final ProgressDialog loading = ProgressDialog.show(POIFormActivity.this, "", "Please wait...", false, false);

    _name = name.getText().toString();
    _location = location.getText().toString();
    _city = city.getText().toString();
    _state = state.getText().toString();
    _pin = pin.getText().toString();

    try {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String URL = ServerLinks.SUBMIT+"/" + userID;

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("FINAL_SUBMIT_RESPONSE", response);
                loading.dismiss();
                try {
                    JSONObject object = new JSONObject(response);
                    Log.d("JSON RESPONSE",object.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                loading.dismiss();
                VolleyLog.d("RESULT_ERROR", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name", _name);
                params.put("categories", categoryID);
                params.put("city", _city);
                params.put("location", _location);
                params.put("state",_state);
                params.put("pincode",_pin);
                params.put("image",EncodePhoto1);
                params.put("image1",EncodePhoto2);
                params.put("image2",EncodePhoto3);
                params.put("latitude",LatitudePOI);
                params.put("longitude",LongitudePOI);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("token", OAuth);
                params.put("x-api-key", ServerLinks.API_KEY);
                return params;
            }

        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                5000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        stringRequest.setShouldCache(false);
        requestQueue.add(stringRequest);


    } catch (Exception e) {
        e.printStackTrace();
    }
}

And Converting the image into Base64 string using this method:

public String ConvertIntoBase64(File filename){
    try{
        InputStream inputStream = new FileInputStream(filename);
        byte[] bytes;
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        bytes = output.toByteArray();
        String encodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
        return encodedString;
    } catch (FileNotFoundException e){
        Toast.makeText(this,e.toString(),Toast.LENGTH_SHORT).show();
        return " ";
    }
}

Can you help me how I can successfully pass the value in the server or what I am doing wrong ? Is there anybetter way to do so ? Thanks in advance.

Piyush Sahay
  • 51
  • 1
  • 10

1 Answers1

2

You are sending base64 encoded string which is big in size. As a result it is taking much time than usual. In that case you have to set a higher time out value.

stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                120000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53