2

I'm having an issue where onResponse(JSONObject response) keeps coming back null.

I have logged the JSONObject.toString being passed as the body in the request (POST), pasted into Postman, and it works; I get a valid response from the server(JSON Object). I have also tried a StringRequest but I'm not having any luck.

Here is my PHP:

public function decryptTest2($param) {
        $key = sodium_hex2bin($param['key']);
        $nonce = sodium_hex2bin($param['nonce']);
        $chiper = sodium_hex2bin($param['chiper']);

        header('Content-Type: application/json; charset=utf-8');

        $plaintext = sodium_crypto_secretbox_open($chiper, $nonce, $key);
        if ($plaintext === false) {
            throw new Exception("Bad ciphertext");
            $json['error'] = "Not able to decrypt.";
            echo json_encode($json['error']);
        }

        $json['success'] = array("decryptedText" => $plaintext);
        echo json_encode($json['success']);

    }

Java:

public void encryptRequest(String input) throws JSONException{

    Encrypt e1 = new Encrypt();
    SodiumAndroid sodium = new SodiumAndroid();
    final LazySodiumAndroid lazySodium = new LazySodiumAndroid(sodium, StandardCharsets.UTF_8);
    SecretBox.Lazy secretBoxLazy = (SecretBox.Lazy) lazySodium;

    final byte[] nonce = lazySodium.randomBytesBuf(SecretBox.NONCEBYTES);
    final Key key = secretBoxLazy.cryptoSecretBoxKeygen();

    final String encryptedText = e1.encrypt(input, nonce, key);

    JSONObject postMethod = new JSONObject();
    postMethod.put("method", "decryptTest2");

    JSONObject postParams = new JSONObject();
    postParams.put("key", key.getAsHexString());
    postParams.put("nonce", lazySodium.sodiumBin2Hex(nonce));
    postParams.put("chiper", encryptedText);

    postMethod.put("params", postParams);

    final String requestBody = postMethod.toString();

    //{"method":"decryptTest2","params":{"key":"E9C0828178CEC39A8AD713F12E36203AA31C8F4885B6AE55B0C033D592CE9075","nonce":"94B683D94504D86D3FA654652545CAD3AA3F4863640ABD91","chiper":"8ED8B09269A0CABE6F8F4D5C851B4B35E987892956EF7E"}}
    //System.out.println("json: " + requestBody);

    String URL = "http://api.domain.com/index.php";

    JsonObjectRequest req = new JsonObjectRequest( Request.Method.POST, URL, postMethod,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println("json response: " + response);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(),
                            Toast.LENGTH_LONG).show();

                    System.out.println("json response: " + error.getMessage());
                }
            }) {

        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json; charset=utf-8");
            return params;
        }

        @Override
        public byte[] getBody() {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                        requestBody, "utf-8");
                return null;
            }
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            String responseString;
            JSONObject jsonObject = null;

            if (response != null) {
                try {
                    responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));

                    jsonObject = new JSONObject(responseString);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
        }

    };

    req.setRetryPolicy(new DefaultRetryPolicy(5000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    requestQueue.add(req);


}

What am I doing wrong?

jaypee
  • 84
  • 4

1 Answers1

1

Ok, after hours and hours of trying to figure out what I was doing wrong, it seems like 'JsonObjectRequest' was not what I was looking for. Although, according to https://developer.android.com/training/volley/request ,

JsonObjectRequest—A request for retrieving a JSONObject response body at a given URL, allowing for an optional JSONObject to be passed in as part of the request body.

My initial thoughts: I'm sending(POST) a JSONObject in the body() and receiving a JSONObject response from the server, so, why not go this route, right? Wrong. I tried passing a JSONObject as an argument and also tried overriding the body() method but I kept receiving a 'null' response in Android while Postman worked without any issues.

My solution: StringRequest. I also manually created the body string instead of creating a JSONObject:

public void encryptRequest(String input) {

    Encrypt sodium = new Encrypt();

    final byte[] nonce = sodium.randomNonce();
    final Key key = sodium.randomKey();

    final String encryptedText = sodium.encrypt(input, nonce, key);

    final String jSonString = "{\"method\":\"decryptTest2\", \"params\":{\"cipher\":\"" +
            encryptedText + "\", \"nonce\":\"" + sodium.convertNonce(nonce) +
            "\", \"key\":\"" + sodium.convertKey(key) + "\"}}";

    StringRequest stringRequest = new StringRequest(Request.Method.POST, sodium.getURL(),
            new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {

                JSONObject jsonObject = new JSONObject(response);

                if (jsonObject.names().get(0).equals("success")) {

                    System.out.println("Decrypted Text: " + jsonObject.getJSONObject("success").getString("decryptedText"));

                }

                else {

                    System.out.println("Error decrypting: " + jsonObject.getJSONObject("error").getString("decryptedText"));

                }


            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("LOG_VOLLEY", e.getMessage());
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("LOG_VOLLEY", error.getMessage());
        }
    }) {
        @Override
        public byte[] getBody() throws AuthFailureError {

            try {
                return jSonString == null ? null : jSonString.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", jSonString, "utf-8");
                return null;
            }
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    requestQueue.add(stringRequest);

}

After doing some research, seems like I'm not the only one having this issue. Anyway, I thought I'd post my solution just in case if someone in the future runs into the same problem.

jaypee
  • 84
  • 4