Skip to content

Commit 3799800

Browse files
committed
1 parent 77d75e5 commit 3799800

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

library/src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public class AsyncHttpClient {
115115
private ThreadPoolExecutor threadPool;
116116
private final Map<Context, List<WeakReference<Future<?>>>> requestMap;
117117
private final Map<String, String> clientHeaderMap;
118+
private boolean isUrlEncodingEnabled = true;
118119

119120

120121
/**
@@ -343,6 +344,7 @@ public void setProxy(String hostname, int port) {
343344
final HttpParams httpParams = this.httpClient.getParams();
344345
httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
345346
}
347+
346348
/**
347349
* Sets the Proxy by it's hostname,port,username and password
348350
*
@@ -351,14 +353,14 @@ public void setProxy(String hostname, int port) {
351353
* @param username the username
352354
* @param password the password
353355
*/
354-
public void setProxy(String hostname,int port,String username,String password){
355-
httpClient.getCredentialsProvider().setCredentials(
356-
new AuthScope(hostname, port),
357-
new UsernamePasswordCredentials(username, password));
356+
public void setProxy(String hostname, int port, String username, String password) {
357+
httpClient.getCredentialsProvider().setCredentials(
358+
new AuthScope(hostname, port),
359+
new UsernamePasswordCredentials(username, password));
358360
final HttpHost proxy = new HttpHost(hostname, port);
359361
final HttpParams httpParams = this.httpClient.getParams();
360362
httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
361-
}
363+
}
362364

363365

364366
/**
@@ -501,7 +503,7 @@ public void head(Context context, String url, AsyncHttpResponseHandler responseH
501503
* @param responseHandler the response handler instance that should handle the response.
502504
*/
503505
public void head(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
504-
sendRequest(httpClient, httpContext, new HttpHead(getUrlWithQueryString(url, params)), null, responseHandler, context);
506+
sendRequest(httpClient, httpContext, new HttpHead(getUrlWithQueryString(isUrlEncodingEnabled, url, params)), null, responseHandler, context);
505507
}
506508

507509
/**
@@ -516,7 +518,7 @@ public void head(Context context, String url, RequestParams params, AsyncHttpRes
516518
* the response.
517519
*/
518520
public void head(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
519-
HttpUriRequest request = new HttpHead(getUrlWithQueryString(url, params));
521+
HttpUriRequest request = new HttpHead(getUrlWithQueryString(isUrlEncodingEnabled, url, params));
520522
if (headers != null) request.setHeaders(headers);
521523
sendRequest(httpClient, httpContext, request, null, responseHandler,
522524
context);
@@ -568,7 +570,7 @@ public void get(Context context, String url, AsyncHttpResponseHandler responseHa
568570
* @param responseHandler the response handler instance that should handle the response.
569571
*/
570572
public void get(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
571-
sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(url, params)), null, responseHandler, context);
573+
sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(isUrlEncodingEnabled, url, params)), null, responseHandler, context);
572574
}
573575

574576
/**
@@ -583,7 +585,7 @@ public void get(Context context, String url, RequestParams params, AsyncHttpResp
583585
* the response.
584586
*/
585587
public void get(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
586-
HttpUriRequest request = new HttpGet(getUrlWithQueryString(url, params));
588+
HttpUriRequest request = new HttpGet(getUrlWithQueryString(isUrlEncodingEnabled, url, params));
587589
if (headers != null) request.setHeaders(headers);
588590
sendRequest(httpClient, httpContext, request, null, responseHandler,
589591
context);
@@ -802,7 +804,7 @@ public void delete(Context context, String url, Header[] headers, AsyncHttpRespo
802804
* @param responseHandler the response handler instance that should handle the response.
803805
*/
804806
public void delete(Context context, String url, Header[] headers, RequestParams params, AsyncHttpResponseHandler responseHandler) {
805-
HttpDelete httpDelete = new HttpDelete(getUrlWithQueryString(url, params));
807+
HttpDelete httpDelete = new HttpDelete(getUrlWithQueryString(isUrlEncodingEnabled, url, params));
806808
if (headers != null) httpDelete.setHeaders(headers);
807809
sendRequest(httpClient, httpContext, httpDelete, null, responseHandler, context);
808810
}
@@ -830,7 +832,20 @@ protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, Ht
830832
}
831833
}
832834

833-
public static String getUrlWithQueryString(String url, RequestParams params) {
835+
public void setURLEncodingEnabled(boolean enabled) {
836+
this.isUrlEncodingEnabled = enabled;
837+
}
838+
839+
/**
840+
* Will encode url, if not disabled, and adds params on the end of it
841+
*
842+
* @param url String with URL, should be valid URL without params
843+
* @param params RequestParams to be appended on the end of URL
844+
*/
845+
public static String getUrlWithQueryString(boolean isUrlEncodingEnabled, String url, RequestParams params) {
846+
if (isUrlEncodingEnabled)
847+
url = url.replace(" ", "%20");
848+
834849
if (params != null) {
835850
String paramString = params.getParamString();
836851
if (!url.contains("?")) {

0 commit comments

Comments
 (0)