Convert a JSONObject into an HTTP header. A request header must contain { Method: "POST" (for example), "Request-URI": "/" (for example), "HTTP-Version": "HTTP/1.1" (for example) } A response header must contain { "HTTP-Version": "HTTP/1.1" (for example), "Sta
(JSONObject jo)
| 146 | * @throws JSONException if the object does not contain enough information. |
| 147 | */ |
| 148 | public static String toString(JSONObject jo) throws JSONException { |
| 149 | Iterator<String> keys = jo.keys(); |
| 150 | String string; |
| 151 | StringBuilder sb = new StringBuilder(); |
| 152 | if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { |
| 153 | sb.append(jo.getString("HTTP-Version")); |
| 154 | sb.append(' '); |
| 155 | sb.append(jo.getString("Status-Code")); |
| 156 | sb.append(' '); |
| 157 | sb.append(jo.getString("Reason-Phrase")); |
| 158 | } else if (jo.has("Method") && jo.has("Request-URI")) { |
| 159 | sb.append(jo.getString("Method")); |
| 160 | sb.append(' '); |
| 161 | sb.append('"'); |
| 162 | sb.append(jo.getString("Request-URI")); |
| 163 | sb.append('"'); |
| 164 | sb.append(' '); |
| 165 | sb.append(jo.getString("HTTP-Version")); |
| 166 | } else { |
| 167 | throw new JSONException("Not enough material for an HTTP header."); |
| 168 | } |
| 169 | sb.append(CRLF); |
| 170 | while (keys.hasNext()) { |
| 171 | string = keys.next(); |
| 172 | if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && !"Reason-Phrase".equals(string) && !"Method".equals(string) && !"Request-URI".equals(string) && !jo.isNull(string)) { |
| 173 | sb.append(string); |
| 174 | sb.append(": "); |
| 175 | sb.append(jo.getString(string)); |
| 176 | sb.append(CRLF); |
| 177 | } |
| 178 | } |
| 179 | sb.append(CRLF); |
| 180 | return sb.toString(); |
| 181 | } |
| 182 | } |