sampathd
sampathd

Reputation: 21

when sending post request with codeName one, Json String is empty

I am trying to send a POST request with a JSON BODY in CodeName one.

It reaches the server with an empty Json String.

Here is the code that makes the connection and sends the message:

JSONObject json = new JSONObject();

class MyConnection extends ConnectionRequest {
    public Map<String, Object> results;

    @Override
    protected void readResponse(InputStream input) throws IOException {
        JSONParser jp = new JSONParser();
        results = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
    }

    @Override
    protected void handleErrorResponseCode(int code, String message) {
        showError("The server returned the error code: " + code);
    }

    @Override
    protected void handleException(Exception err) {
        showError("There was a connection error: " + err);
    }

    @Override
    protected void postResponse() {
        try {
            json.put("AAA", "AAA");
            json.put("BBB", "BBB");
            json.put("CCC", "CCC");
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
        os.write(json.toString().getBytes("UTF-8"));
    }
}

---

MyConnection connec = new MyConnection ();

connec.setUrl("http://testServoce/addUser");
connec.setPost(true);
connec.setContentType("application/json");

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
connec.setDisposeOnCompletion(dlg);

NetworkManager.getInstance().addToQueueAndWait(connec);

Upvotes: 2

Views: 105

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

I'm not sure what your intention was but it looks like you misunderstood the goal of the postResponse method. It's unrelated to the POST web method and is just called after the response completed on the EDT. So changing the JSON value there is irrelevant.

Also it looks like you are using two separate JSON parsers for some reason. The builtin one and the org.json one from one of the cn1libs.

Upvotes: 1

Related Questions