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