I have the following as part of a method that is basically grabbing a webpage, I want to map the resulting header and body to JSON, the body is spat out as a string but ideally I want the header values split into key value so in the JavaScript I can access them directly.
Map<String, String> assoc = new HashMap<String, String>();
Map<String, List<String>> headerMap = new LinkedHashMap<String, List<String>>();
headerMap = connection.getHeaderFields();
for (String key : headerMap.keySet()) {
assoc.put(key, headerMap.get(key).toString());
}
JSONObject returnObj = new JSONObject();
returnObj.put("header", assoc);
returnObj.put("body", sb.toString());
return returnObj.toString(); //Set digit to add indent spacing.
This unfortunately returns the header as a string rather than an array...
{"header":"{cache-control=[max-age=0], content-type=[text\/html], connection=[Keep-Alive],
Ideally this would be more like (friendlier for javascript)...
{
"headers": [
{"test": "testval"},
{"testb": "testbval"}
]
}