Skip to content

[ANDROID][volley] Handle UnsupportedEncodingException in invokeAPI #6436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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