From 03ed192b98534e8fcfb85a4f6613cc746d1d1295 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 14 Oct 2016 11:12:37 +0800 Subject: [PATCH] [Android] fix URL encoding for Android API client (#3992) * fix url encoding for android * add exception handling for url encoding in android --- .../src/main/resources/android/apiInvoker.mustache | 10 +++++++--- .../android/libraries/volley/apiInvoker.mustache | 7 ++++++- .../src/main/java/io/swagger/client/ApiInvoker.java | 10 +++++++--- .../src/main/java/io/swagger/client/ApiInvoker.java | 7 ++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/android/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/android/apiInvoker.mustache index 0b3f24c9f0..0dac1bb8f5 100644 --- a/modules/swagger-codegen/src/main/resources/android/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/android/apiInvoker.mustache @@ -16,6 +16,8 @@ import org.apache.http.params.*; import org.apache.http.util.EntityUtils; import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; import java.net.URLEncoder; @@ -25,8 +27,6 @@ import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; -import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.KeyManagementException; @@ -207,7 +207,11 @@ public class ApiInvoker { } public String escapeString(String str) { - return str; + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return str; + } } public static Object deserialize(String json, String containerType, Class cls) throws ApiException { diff --git a/modules/swagger-codegen/src/main/resources/android/libraries/volley/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/android/libraries/volley/apiInvoker.mustache index e40f4c0412..d57d8bb058 100644 --- a/modules/swagger-codegen/src/main/resources/android/libraries/volley/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/android/libraries/volley/apiInvoker.mustache @@ -20,6 +20,7 @@ import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -232,7 +233,11 @@ public class ApiInvoker { } public String escapeString(String str) { - return str; + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return str; + } } public static Object deserialize(String json, String containerType, Class cls) throws ApiException { diff --git a/samples/client/petstore/android/httpclient/src/main/java/io/swagger/client/ApiInvoker.java b/samples/client/petstore/android/httpclient/src/main/java/io/swagger/client/ApiInvoker.java index 24f5a12978..7118a48d50 100644 --- a/samples/client/petstore/android/httpclient/src/main/java/io/swagger/client/ApiInvoker.java +++ b/samples/client/petstore/android/httpclient/src/main/java/io/swagger/client/ApiInvoker.java @@ -39,6 +39,8 @@ import org.apache.http.params.*; import org.apache.http.util.EntityUtils; import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; import java.net.URLEncoder; @@ -48,8 +50,6 @@ import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; -import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.KeyManagementException; @@ -230,7 +230,11 @@ public class ApiInvoker { } public String escapeString(String str) { - return str; + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return str; + } } public static Object deserialize(String json, String containerType, Class cls) throws ApiException { diff --git a/samples/client/petstore/android/volley/src/main/java/io/swagger/client/ApiInvoker.java b/samples/client/petstore/android/volley/src/main/java/io/swagger/client/ApiInvoker.java index 158209c4be..e1e32e370a 100644 --- a/samples/client/petstore/android/volley/src/main/java/io/swagger/client/ApiInvoker.java +++ b/samples/client/petstore/android/volley/src/main/java/io/swagger/client/ApiInvoker.java @@ -43,6 +43,7 @@ import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -246,7 +247,11 @@ public class ApiInvoker { } public String escapeString(String str) { - return str; + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return str; + } } public static Object deserialize(String json, String containerType, Class cls) throws ApiException {