5

I am using the nanohttpd server in my project. The GET Request should send a JSON file as response but nanohttpd only allows String. How can I do this?

This is the GET method:

class GetHandler{
JSONObject response;
JSONObject doGet(Queue queue){
    Map.Entry<String, Message> entry = (Map.Entry<String, Message>)queue.getQueue().entrySet().iterator().next();
    String key = entry.getKey();
    String value = entry.getValue().getText();
    int reCount = entry.getValue().increaseCount();
    queue.getQueue().remove(key);
    Date date = new Date();
    long time = date.getTime();
    queue.getInFlightQueue().put(key,new Message(value, reCount, time));
    System.out.println("ID : " + key + " Message Body : " + value);
    response.put("ID",key);
    response.put("Message Body", value);
    return response;
}
}

This is the Handling part:

if (uriComponents[1].equals("queue") && mainHashMap.containsKey(uriComponents[2])) {
    if (mainHashMap.get(uriComponents[2]).getQueue().isEmpty()) {
        response = "Error : trying to access an empty queue";
        return newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", response);
   } else {
        JSONObject message = implementGet.doGet(mainHashMap.get(uriComponents[2]));
        return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "text/plain", message.toJSONString());
        }
} else {
            response = "Error : Given queue name doesn't exits. Usage:- GET/queue/<queue_name>";
            return newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", response);
}
5
  • JSON is a textual format so you can stringify it to set it in the response of the GET (I suppose, or use a POST if you need to send the JSON with the request). Commented May 8, 2018 at 6:25
  • I've tried that but it doesn't work. When I convert it to a string, the GET request doesn't return anything. Commented May 8, 2018 at 6:40
  • Can you show us ? It is hard to help without seeing what you are doing for the moment. Use the edit to add a snippet of your code. See How to Ask, without more information I can't help and the question would end up closed as too broad. That's not your first question, you know how that works ;) Commented May 8, 2018 at 6:40
  • First thing, I see you use text/plain, try with application/json instead. (even if this is a message.toJSONString() used, The message will know that the content is a JSON. Then, what is the result of this solution ? Is the JSON correct ' (what is the result of message.toJSONString()). FYI: I don't know nanoHTTP so that part is left for others to check. Commented May 8, 2018 at 6:52
  • I've tried it both ways obviously but neither of them work. Commented May 8, 2018 at 6:59

1 Answer 1

2

Instead of using a JSON object, I used a JSON String something like "{\"id\":\"abcd\", \"message\",\"1234\"}" and it worked.

Sign up to request clarification or add additional context in comments.

1 Comment

Update: I used a JSON Builder eventually and converted that to a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.