#631 remove IllegalArgumentException of HttpRequestBody.json and so on

master
yihua.huang 2017-07-22 11:49:23 +08:00
parent 486a6d5c93
commit c3bdb20458
1 changed files with 19 additions and 7 deletions

View File

@ -64,24 +64,36 @@ public class HttpRequestBody implements Serializable {
this.encoding = encoding; this.encoding = encoding;
} }
public static HttpRequestBody json(String json, String encoding) throws UnsupportedEncodingException { public static HttpRequestBody json(String json, String encoding) {
return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding); try {
return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
}
} }
public static HttpRequestBody xml(String xml, String encoding) throws UnsupportedEncodingException { public static HttpRequestBody xml(String xml, String encoding) {
return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding); try {
return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
}
} }
public static HttpRequestBody custom(byte[] body, String contentType, String encoding) throws UnsupportedEncodingException { public static HttpRequestBody custom(byte[] body, String contentType, String encoding) {
return new HttpRequestBody(body, contentType, encoding); return new HttpRequestBody(body, contentType, encoding);
} }
public static HttpRequestBody form(Map<String,Object> params, String encoding) throws UnsupportedEncodingException { public static HttpRequestBody form(Map<String,Object> params, String encoding){
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size()); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) { for (Map.Entry<String, Object> entry : params.entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
} }
return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding); try {
return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
}
} }
public byte[] getBody() { public byte[] getBody() {