When sending my JSON response I get in visible form the content-type. This is an issue because when I parse the JSON on the other side it includes the headers and it can't parse back into a hashmap.
How should I encode this response so that the headers don't "show"
Here is the method that generates the response
/**
* Sends reply back to client
* @throws Exception
*/
private void sendResponse() throws Exception{
//Content type
String contentTypeLine = "Content-Type: text/json" + "\r\n";
//Create dummy JSON object
HashMap<String, String> mapResponse = new HashMap<String, String>();
mapResponse.put("Author", "James");
mapResponse.put("Author 2", "John");
//Convert to JSON
Gson gson = new Gson();
String json = gson.toJson(mapResponse);
//Set type
responseToClient.writeBytes(contentTypeLine);
//Set JSON
responseToClient.writeBytes(json);
}
Here is the sample response
Content-Type: text/json
{"Author":"James","Author 2":"John"}
Request getting code
/**
* Code to execute on thread
*/
public void run(){
try {
//Log new client
System.out.println("The client " + connectedClient.getInetAddress() +
":" + connectedClient.getPort() + " is connected");
//Get the client request
clientRequest = new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));
//Start response object
responseToClient = new DataOutputStream(connectedClient.getOutputStream());
//Process the request
processClientRequest();
//Close buffered writer
responseToClient.close();
} catch (Exception e) {
//Print error
e.printStackTrace();
}
}
Thank you!