Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd1855f

Browse files
committed
Provided setter for encoding in RequestParams, closes android-async-http#406
1 parent 95258be commit cd1855f

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

library/src/main/java/com/loopj/android/http/RequestParams.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.loopj.android.http;
2020

21+
import android.util.Log;
22+
2123
import org.apache.http.HttpEntity;
2224
import org.apache.http.client.entity.UrlEncodedFormEntity;
2325
import org.apache.http.client.utils.URLEncodedUtils;
@@ -88,12 +90,27 @@
8890
*/
8991
public class RequestParams {
9092

93+
protected final static String LOG_TAG = "RequestParams";
9194
protected boolean isRepeatable;
9295
protected boolean useJsonStreamer;
9396
protected ConcurrentHashMap<String, String> urlParams;
9497
protected ConcurrentHashMap<String, StreamWrapper> streamParams;
9598
protected ConcurrentHashMap<String, FileWrapper> fileParams;
9699
protected ConcurrentHashMap<String, Object> urlParamsWithObjects;
100+
protected String contentEncoding = HTTP.UTF_8;
101+
102+
/**
103+
* Sets content encoding for return value of {@link #getParamString()} and {@link
104+
* #createFormEntity()} <p>&nbsp;</p> Default encoding is "UTF-8"
105+
*
106+
* @param encoding String constant from {@link org.apache.http.protocol.HTTP}
107+
*/
108+
public void setContentEncoding(final String encoding) {
109+
if (encoding != null)
110+
this.contentEncoding = encoding;
111+
else
112+
Log.d(LOG_TAG, "setContentEncoding called with null attribute");
113+
}
97114

98115
/**
99116
* Constructs a new empty {@code RequestParams} instance.
@@ -378,9 +395,10 @@ private HttpEntity createJsonStreamerEntity() throws IOException {
378395

379396
private HttpEntity createFormEntity() {
380397
try {
381-
return new UrlEncodedFormEntity(getParamsList(), HTTP.UTF_8);
398+
return new UrlEncodedFormEntity(getParamsList(), contentEncoding);
382399
} catch (UnsupportedEncodingException e) {
383-
return null; // Actually cannot happen when using utf-8
400+
Log.e(LOG_TAG, "createFormEntity failed", e);
401+
return null; // Can happen, if the 'contentEncoding' won't be HTTP.UTF_8
384402
}
385403
}
386404

@@ -472,7 +490,7 @@ private List<BasicNameValuePair> getParamsList(String key, Object value) {
472490
}
473491

474492
protected String getParamString() {
475-
return URLEncodedUtils.format(getParamsList(), HTTP.UTF_8);
493+
return URLEncodedUtils.format(getParamsList(), contentEncoding);
476494
}
477495

478496
public static class FileWrapper {

0 commit comments

Comments
 (0)