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), "Status-Code"
(JSONObject jo)
| 125 | * information. |
| 126 | */ |
| 127 | public static String toString(JSONObject jo) throws JSONException { |
| 128 | @SuppressWarnings("rawtypes") |
| 129 | Iterator keys = jo.keys(); |
| 130 | String string; |
| 131 | StringBuffer sb = new StringBuffer(); |
| 132 | if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { |
| 133 | sb.append(jo.getString("HTTP-Version")); |
| 134 | sb.append(' '); |
| 135 | sb.append(jo.getString("Status-Code")); |
| 136 | sb.append(' '); |
| 137 | sb.append(jo.getString("Reason-Phrase")); |
| 138 | } else if (jo.has("Method") && jo.has("Request-URI")) { |
| 139 | sb.append(jo.getString("Method")); |
| 140 | sb.append(' '); |
| 141 | sb.append('"'); |
| 142 | sb.append(jo.getString("Request-URI")); |
| 143 | sb.append('"'); |
| 144 | sb.append(' '); |
| 145 | sb.append(jo.getString("HTTP-Version")); |
| 146 | } else { |
| 147 | throw new JSONException("Not enough material for an HTTP header."); |
| 148 | } |
| 149 | sb.append(CRLF); |
| 150 | while (keys.hasNext()) { |
| 151 | string = keys.next().toString(); |
| 152 | if (!string.equals("HTTP-Version") && !string.equals("Status-Code") && |
| 153 | !string.equals("Reason-Phrase") && !string.equals("Method") && |
| 154 | !string.equals("Request-URI") && !jo.isNull(string)) { |
| 155 | sb.append(string); |
| 156 | sb.append(": "); |
| 157 | sb.append(jo.getString(string)); |
| 158 | sb.append(CRLF); |
| 159 | } |
| 160 | } |
| 161 | sb.append(CRLF); |
| 162 | return sb.toString(); |
| 163 | } |
| 164 | } |