2

I have a simple task of uploading a base 64 encoded image to a server using Volley.

My server does not do anything. It just returns a 200 status code. But I always end up with this error even after the server returns 200. I tried some of the other solutions online but nothing seems to work.

private void uploadImage(){
    obj = new JSONObject();
    try {
        obj.put("image", getStringImage(bitmap));
    } catch (Exception e) {

    }
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, UPLOAD_URL, obj,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Log.e("Message from server", jsonObject.toString());
                    Toast.makeText(getApplication(), "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.e("Message from server", volleyError.toString());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            return headers;
        }

    };
    requestQueue.add(jsonObjectRequest);
}

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}
user3282666
  • 640
  • 1
  • 7
  • 21

1 Answers1

1

I know this is late , but for me , the solution is to increase the http max post size in your server.for me I using spring boot with embedded tomcat server so the solution is to add the below line to your application.properties file:

server.tomcat.max-http-post-size=10000000

above I have set it to 10MB , another way of doing this is through java code as pointed by the answer in this link Increase HTTP Post maxPostSize in Spring Boot

hope that helps you

Mohammed Fathi
  • 1,237
  • 1
  • 14
  • 12