0

The code which makes the JSON object and calls the Async method:

final String OWNER_PASSWORD="ownerPassword";
final String OWNER_EMAIL="ownerEmailId";

    private void CheckPassword() {

    shopkeeperJSON = new JSONObject();
    try{
        shopkeeperJSON.put(OWNER_EMAIL,"varuncr7raj@gmail.com");
        shopkeeperJSON.put(OWNER_PASSWORD,"1");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    new POST_Request(this).execute("http://192.168.1.5:3000/api/CheckPassword", shopkeeperJSON.toString());
}

Here is the Android code of post method it is sending a JSON object to my local server:

@Override
protected String doInBackground(String... params) {

    try {
        URL url = new URL(params[0]);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod(REQUEST_METHOD);
        httpCon.setRequestProperty("Accept","application/json");
        //httpCon.setRequestProperty("Content-Type", "application/json");
        httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        //Input
        OutputStream os = httpCon.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write(params[1]);
        //osw.write("{ ownerEmailId : varuncr7raj@gmail.com, ownerPassword: 1 }");
        osw.flush();
        osw.close();
        os.close();  //don't forget to close the OutputStream
        httpCon.connect();

        //read the inputstream and print it
        String result;
        BufferedInputStream bis = new BufferedInputStream(httpCon.getInputStream());
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result2 = bis.read();
        while(result2 != -1) {
            buf.write((byte) result2);
            result2 = bis.read();
        }
        result = buf.toString();
        //System.out.println(result);
        Log.e("result", result);
        httpCon.disconnect();
        return result;

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

    return result;
}

Here is the code of my node js server. Which is just logging the res.body of the request.

exports.checkPassword = function(req,res){
var data = JSON.stringify(req.body);
//console.log(JSON.stringify(req.body));
var obj = JSON.parse(data);
console.log(obj);
res.send(req.body);
};

The output of the code:-

{ '{"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}': '' }

As you can see the output the JSON object is becoming the JSON key for the JSON object. Thank you! Any help will be appreciated!

Desired Output:

 {"ownerEmailId":"varuncr7raj@gmail.com","ownerPassword":"1"}
OhhhThatVarun
  • 2,305
  • 1
  • 15
  • 33

1 Answers1

0

You are telling that you'll be sending the params as x-www-form-urlencoded but you are sending a JSON instead. Change to httpCon.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

On the server side try just using the properties you want from req.body:

exports.checkPassword = function(req, res) {
    const { ownerEmailId, ownerPassword } = req.body;
    console.log(ownerEmailId, ownerPassword);
    res.send(req.body);
};
Danilo Lemes
  • 1,434
  • 10
  • 12