0

I am trying to write code to send jpg images to a server that can receive JSON requests. This images are stored on the device. I am basically porting a Python code that uses Requests: HTTP for Humans to communicate with a server. So far I managed to establish the communication with the code I pasted below. So I can see some hope.

     // JAVA code on Android
     String URL = BASE_URL + "/relative_url";

     JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, URL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "Successful POST request");
                    try {
                        sceneid = response.getInt("id");
                        Log.i(TAG, "Scene id: " + sceneid);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(TAG, "Bad POST request");
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    String credentials = "myuser:mypass";
                    String auth = "Basic " +  Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                    headers.put("Authorization", auth);
                    return headers;
                }
    };
    queue.add(postRequest);

However, I am not able to replicate the Python code below. I already checked thoroughly for a solution online, but I did not manage to make it work. The solutions I found either use deprecated libraries or the code does not work. The command in python I am trying to replicate is:

# Python code to replicate in JAVA
auth = {'myuser', 'mypass'}
file = {'jpgdata': open('myimg.jpg','rb')}
req = requests.post(BASE_URL + '/relative_url', params={'param', param}, files=file, auth=auth)

Some help will be really appreciated.

Thank you in advance.

Poiex
  • 138
  • 10

2 Answers2

0

You will need to read the bytes from that File into a byte[] and put that object into your JSONObject.

You should also have a look at the following posts :

ByteArray in JSON

Binary Data in JSON String. Something better than Base64

BSON library for java

Hope this helps.

Community
  • 1
  • 1
kgsharathkumar
  • 1,369
  • 1
  • 19
  • 36
0
 public static String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = null;
        try {
            System.gc();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            b = baos.toByteArray();
            temp = Base64.encodeToString(b, Base64.DEFAULT);
            Log.e("EWN", "Out of memory error catched");
        }
        return temp;
    }

Use this code and send string of image to server. Hope help you.

Moeen Kashisaz
  • 302
  • 1
  • 3
  • 13
  • I tried to implement this part. I first convert the image in bitmap "bitmap = BitmapFactory.decodeFile(imgPath);" and then I apply your function "String image = BitMapToString(bitmap);". Then in the parameters I add it in this way: "params.put("jpgdata", image);" However, I keep having error 400. – Poiex Dec 12 '16 at 13:02