Skip to content

Commit

Permalink
[ANDROID][volley] Handle UnsupportedEncodingException in invokeAPI (#…
Browse files Browse the repository at this point in the history
…6436)

* [ANDROID][volley] Handle UnsupportedEncodingException in invokeAPI (Issue #6432)
The constructor of StringEntity can throw UnsupportedEncodingException, which is not catch nor thrown by createRequest method.
Therefore the build of android client fails with:

ApiInvoker.java:448: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown

This commit adds a try ... catch on UnsupportedEncodingException in invokeAPI methods and declare this exception to be thrown in createRequest

* [ANDROID][volley] Handle UnsupportedEncodingException in invokeAPI (Issue #6432)
The constructor of StringEntity can throw UnsupportedEncodingException, which is not catch nor thrown by createRequest method.
Therefore the build of android client fails with:

ApiInvoker.java:448: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown

This commit adds a try ... catch on UnsupportedEncodingException in invokeAPI methods and declare this exception to be thrown in createRequest

* [ANDROID][Volley] Handle UnsupportedEncodingException (Issue #6432)
Throw more precise ApiException
  • Loading branch information
ecolleu authored and wing328 committed Sep 7, 2017
1 parent 0cf82d7 commit 3cd3b90
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,32 @@ public class ApiInvoker {
}

public String invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
RequestFuture<String> future = RequestFuture.newFuture();
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
if(request != null) {
mRequestQueue.add(request);
return future.get(connectionTimeout, TimeUnit.SECONDS);
} else {
return "no data";
try {
RequestFuture<String> future = RequestFuture.newFuture();
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
if(request != null) {
mRequestQueue.add(request);
return future.get(connectionTimeout, TimeUnit.SECONDS);
} else {
return "no data";
}
} catch (UnsupportedEncodingException ex) {
throw new ApiException(0, "UnsupportedEncodingException");
}
}

public void invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException {
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, stringRequest, errorListener);
if (request != null) {
mRequestQueue.add(request);
try {
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, stringRequest, errorListener);
if (request != null) {
mRequestQueue.add(request);
}
} catch (UnsupportedEncodingException ex) {
throw new ApiException(0, "UnsupportedEncodingException");
}
}

public Request<String> createRequest(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException {
public Request<String> createRequest(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException, UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
b.append("?");
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/android/volley/docs/StoreApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ This endpoint does not need any parameter.

### Return type

[**Map&lt;String, Integer&gt;**](Map.md)
**Map&lt;String, Integer&gt;**

### Authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,24 +365,32 @@ private void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map
}

public String invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
RequestFuture<String> future = RequestFuture.newFuture();
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
if(request != null) {
mRequestQueue.add(request);
return future.get(connectionTimeout, TimeUnit.SECONDS);
} else {
return "no data";
try {
RequestFuture<String> future = RequestFuture.newFuture();
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
if(request != null) {
mRequestQueue.add(request);
return future.get(connectionTimeout, TimeUnit.SECONDS);
} else {
return "no data";
}
} catch (UnsupportedEncodingException ex) {
throw new ApiException(0, "UnsupportedEncodingException");
}
}

public void invokeAPI(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException {
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, stringRequest, errorListener);
if (request != null) {
mRequestQueue.add(request);
try {
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, stringRequest, errorListener);
if (request != null) {
mRequestQueue.add(request);
}
} catch (UnsupportedEncodingException ex) {
throw new ApiException(0, "UnsupportedEncodingException");
}
}

public Request<String> createRequest(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException {
public Request<String> createRequest(String host, String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType, String[] authNames, Response.Listener<String> stringRequest, Response.ErrorListener errorListener) throws ApiException, UnsupportedEncodingException {
StringBuilder b = new StringBuilder();
b.append("?");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public Map<String, Integer> getInventory () throws TimeoutException, ExecutionEx
try {
String localVarResponse = apiInvoker.invokeAPI (basePath, path, "GET", queryParams, postBody, headerParams, formParams, contentType, authNames);
if (localVarResponse != null) {
return (Map<String, Integer>) ApiInvoker.deserialize(localVarResponse, "map", Map.class);
return (Map<String, Integer>) ApiInvoker.deserialize(localVarResponse, "map", Integer.class);
} else {
return null;
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public void getInventory (final Response.Listener<Map<String, Integer>> response
@Override
public void onResponse(String localVarResponse) {
try {
responseListener.onResponse((Map<String, Integer>) ApiInvoker.deserialize(localVarResponse, "map", Map.class));
responseListener.onResponse((Map<String, Integer>) ApiInvoker.deserialize(localVarResponse, "map", Integer.class));
} catch (ApiException exception) {
errorListener.onErrorResponse(new VolleyError(exception));
}
Expand Down

0 comments on commit 3cd3b90

Please sign in to comment.