PATCH Verb in Android (OkHttp, Volley, Retrofit...) - android

I know a similar question has been asked a few times now, but I can't seem to find a solution for a simple problem: PATCH verb.
So I ask anyone who as solved the PATCH issue in Android either by using OkHttp, Volley or Retrofit ( or if you used a different method or library, please share).
I have spent 3 days digging around and I can't find any solutions to this problem. All I am trying to do is to do a partial update to a table via RESTful API which is built and ran in .NET environment.
I am currently using Volley, but every time I run the app, I get the exception error that says something along the lines of PATCH is not an allowed...? and it then gives a array of other verbs that can be used such as "POST, PUT, GET...". So, not very helpful at all.
I have tried using OkHttp like this:
JSONObject infoRequestBody = new JSONObject();
try {
infoRequestBody.put("Salutation", "MRrr.");
infoRequestBody.put("FirstName", "Work Now!!");
infoRequestBody.put("LastName", "Test from my mobile again!! Okhttp");
infoRequestBody.put("MiddleName", "From the Mobile");
infoRequestBody.put("NickName", null);
infoRequestBody.put("BirthDate", "1978-11-23T00:00:00.000Z");
infoRequestBody.put("CompanyName", null);
// infoRequestBody.put("RegDate", "2015-01-18T23:39:13.873Z");
infoRequestBody.put("Gender", "Male");
infoRequestBody.put("IsPregnant", null);
infoRequestBody.put("IsNursing", null);
infoRequestBody.put("WorkPhone", null);
infoRequestBody.put("WorkPhoneExt", null);
infoRequestBody.put("PhoneNumber", null);
infoRequestBody.put("Address", "My android app working!!");
infoRequestBody.put("Address2", null);
infoRequestBody.put("City", null);
infoRequestBody.put("State", "CA");
infoRequestBody.put("ZipCode", null);
infoRequestBody.put("EmailAddress", "email#me.com");
infoRequestBody.put("Occupation", null);
infoRequestBody.put("CountryofResidence", "United States");
infoRequestBody.put("CountryCode", "US");
infoRequestBody.put("Ethnicity", "Asian");
infoRequestBody.put("ModifiedDate", "2015-01-18T23:39:13.873Z");
infoRequestBody.put("ModifiedBy", null);
infoRequestBody.put("AcceptedTerms", true);
infoRequestBody.put("AcceptedPrivacyPolicyCheckbox", false);
infoRequestBody.put("AcceptedUnder18ConsentCheckbox", false);
} catch (JSONException e1) {
e1.printStackTrace();
}
RequestBody body = com.squareup.okhttp.RequestBody.create(JSON, infoRequestBody.toString());
com.squareup.okhttp.Request.Builder request = new com.squareup.okhttp.Request.Builder()
.url("APIUrls.PATIENT_INFO_URL")
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("X-ZUMO-APPLICATION", APIUrls.ZUMO_KEY)
.patch(body);
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(com.squareup.okhttp.Request request, IOException e) {
}
#Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code" + response);
Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
System.out.println(response.body().string());
}
});
but client.newCall(request).enque(new Callback()... gives me an error saying that the request can not be applied to Request.Builder.
I am new to OkHttp, by the way.
Anyways, I have been struggling to get PATCH to work for far too long. Maybe I am shooting in the dark, but if anyone out there has any suggestions, thoughts on how to get 'PATCH` to work, I'd be forever grateful to you.
Thanks for your help.

As GreyBeardedGeek says, you can use Retrofit + OkHttp to implement this.
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient())
.setEndpoint(ENDPOINT)
.build();
MyPatchService patchService = restAdapter.create(MyPatchService.class);
UserUpdate updatedUser = new UserUpdate();
updatedUser.Salutation = "Mr";
updatedUser.FirstName = "Adam";
patchService.update(updatedUser);
And MyPatchService looks something like this:
public interface MyPatchService {
#PATCH("/some/update/endpoint")
User update(UserUpdate updatedObject);
}
Your UserUpdate class could look like this:
public class UserUpdate {
public String Salutation;
public String FirstName;
public String LastName;
}
If you do this, the JSON that will be sent will look like this (note that LastName is not serialized since it was never set - this is configurable through supplying a custom Gson instance to Retrofit):
{
"Salutation": "Mr",
"FirstName": "Adam"
}
The request/response objects are plain Java representations of the objects you send/receive over the network. Being a patch request, you can make your member vars Integer, Boolean, etc and they won't get serialized if not set (i.e. if they're null).

I haven't tried this, but apparently, if you use Retrofit with the OkHTTP's OKClient, then Retrofit supports an #Patch annotation.
See http://square.github.io/retrofit/javadoc/retrofit/http/PATCH.html and Retrofit & HTTP Patch

Alright! Thanks #Adam and everyone who contributed to get me in the right direction. Really appreciate it. So, here's what I ended up doing (I barely customized what #Adam posted, which of course is the correct answer!):
Firstly,
I set up my UserUpdate class as follows:
public class UserUpdate {
public String Salutation;
public String FirstName;
public String LastName;
public String MiddleName;
public String NickName;
//Setters and Getters...
public String getSalutation() {
return Salutation;
}
public void setSalutation(String salutation) {
Salutation = salutation;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getMiddleName() {
return MiddleName;
}
public void setMiddleName(String middleName) {
MiddleName = middleName;
}
public String getNickName() {
return NickName;
}
public void setNickName(String nickName) {
NickName = nickName;
}
}
and then I set up the interface class called PatientInfoAPI:
public interface PatientInfoAPI{
#PATCH("/tables/PatientInformation/{id}")
public void update(#Body UserUpdate updatedDAta, #Path("id") String id,Callback<UserUpdate> response);
}
Notice I'm passing the table id to be "PATCH'd", and most importantly I have the #Body annotation which allows me to pass the data to the update without getting an error (I tried without it and was getting an error)
And finally inside my main Activity:
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient()) //very important!!!
.setEndpoint(ENDPOINT)//base url
.setRequestInterceptor(new RequestInterceptor() {//You may not need to pass headers, but I do, so this is how it's done.
#Override
public void intercept(RequestFacade request) {
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept", "application/json");
request.addHeader("X-ZUMO-APPLICATION", APIUrls.ZUMO_KEY);
}
})
.build();
PatientInfoAPI patchService = restAdapter.create(PatientInfoAPI.class);
UserUpdate updatedUser = new UserUpdate();
updatedUser.setSalutation("Sr.");
updatedUser.setFirstName("YesSirtItsWorking");
updatedUser.setLastName("Bays");
updatedUser.setMiddleName("ComeOn Now man!");
updatedUser.setNickName("");
//Passing the tableId along with the data to update
patchService.update(updatedUser, APIUrls.TABLE_ID, new retrofit.Callback<UserUpdate>() {
#Override
public void success(UserUpdate userUpdates, retrofit.client.Response response) {
Log.v("All is good", "Nickname:" + userUpdates.getNickName().toString() + " Email: " + userUpdates.getEmailAddress().toString());
}
#Override
public void failure(RetrofitError error) {
Log.v("All is good", error.getLocalizedMessage().toString());
}
});
I know that maybe some of the steps are not very sophisticated, but for those who are starting with Retrofit and OkHttp, and most importantly have been struggling to get PATCH to work with Android, this is the solution. Again, props to #Adam for pretty much guide me through the process. #Adam was the one who really answered this question - I'm just putting it all together for those who may need a to see the full picture. Thanks, #Adam!!! You are a rockstar!

Related

Retrofit call returning 400, cURL request working perfectly fine, syntax issue

I've tried making a retrofit call to an API endpoint, but it's returning a 400 error, however my curl request is working perfectly fine. I can't seem to spot the error, could someone double check my work to see where I made a mistake?
The curl call that works:
curl --request POST https://connect.squareupsandbox.com/v2/payments \
--header "Content-Type: application/json" \
--header "Authorization: Bearer accesstoken112233" \
--header "Accept: application/json" \
--data '{
"idempotency_key": "ab2a118d-53e2-47c6-88e2-8c48cb09bf9b",
"amount_money": {
"amount": 100,
"currency": "USD"},
"source_id": "cnon:CBASEITjGLBON1y5od2lsdxSPxQ"}'
My Retrofit call:
public interface IMakePayment {
#Headers({
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Bearer accesstoken112233"
})
#POST(".")
Call<Void> listRepos(#Body DataDto dataDto);
}
DataDto class:
public class DataDto {
private String idempotency_key;
private String amount_money;
private String source_id;
public DataDto(String idempotency_key, String amount_money, String source_id) {
this.idempotency_key = idempotency_key;
this.amount_money = amount_money;
this.source_id = source_id;
}
}
And lastly making the retrofit call:
DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", "{\"amount\": 100, \"currency\": \"USD\"}", "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");
RetrofitInterfaces.IMakePayment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IMakePayment.class);
Call<Void> call = service.listRepos(dataDto);
call.enqueue(new Callback<Void>() {
#Override
public void onResponse(#NonNull Call<Void> call, #NonNull Response<Void> response) {
Log.d(TAG, "onResponse: " + response.toString());
}
#Override
public void onFailure(#NonNull Call<Void> call, #NonNull Throwable t) {
Log.d(TAG, "onFailure: Error: " + t);
}
});
Retrofit Instance:
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://connect.squareupsandbox.com/v2/payments/";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Edit 1: Changing to second parameter to JSON Object
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("amount", 100);
jsonObject.put("currency", "USD");
}catch (Exception e){
Log.d(TAG, "onCreate: " + e);
}
DataDto dataDto = new DataDto("ab2a118d-53e2-47c6-88e2-8c48cb09bf9b", jsonObject, "cnon:CBASEITjGLBON1y5od2lsdxSPxQ");
First of all, let's see what 400 means
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status
code indicates that the server cannot or will not process the request
due to something that is perceived to be a client error (e.g.,
malformed request syntax, invalid request message framing, or
deceptive request routing).
Now we are sure, the problem stands in our request (not server fault), most probably it is because you are trying to convert JSON in request (do not do this explicitly GSON will convert automatically)
Use interceptor to verify your outgoing network requests (Tell the result here)
you use #POST(".") which does not make sense, please understand BASE_URL is your server URL NOT MORE
The problem could be translating this post request
So a possible solution
Change base URL into "https://connect.squareupsandbox.com/"
Replace #POST(".") with #POST("v2/payments/")
PS. #NaveenNiraula mentioned right thing even though it did not help you, please follow his instruction, it is the correct way parsing data using GSON (make sure you include it and configure it correctly) converter
EDIT
I make it work (I eliminated 400 error code that is what you want as long as question title is concerned) partially which means I detect why 400 error was occurred and fixed it but unfortunately, I stuck the UNAUTHORIZED issue. The problem was relating to converting json and data type
data class DataDTO(
val idempotency_key: String,
val source_id: String,
val amount_money: MoneyAmount
)
data class MoneyAmount(
val amount: Int,
val currency: String
)
I gist all code here you can refer
You need two DTO classes as below:
public class Amount_money
{
private String amount;
private String currency;
public String getAmount ()
{
return amount;
}
public void setAmount (String amount)
{
this.amount = amount;
}
public String getCurrency ()
{
return currency;
}
public void setCurrency (String currency)
{
this.currency = currency;
}
#Override
public String toString()
{
return "ClassPojo [amount = "+amount+", currency = "+currency+"]";
}
}
And
public class DataDto
{
private String idempotency_key;
private Amount_money amount_money;
private String source_id;
public String getIdempotency_key ()
{
return idempotency_key;
}
public void setIdempotency_key (String idempotency_key)
{
this.idempotency_key = idempotency_key;
}
public Amount_money getAmount_money ()
{
return amount_money;
}
public void setAmount_money (Amount_money amount_money)
{
this.amount_money = amount_money;
}
public String getSource_id ()
{
return source_id;
}
public void setSource_id (String source_id)
{
this.source_id = source_id;
}
#Override
public String toString()
{
return "ClassPojo [idempotency_key = "+idempotency_key+", amount_money = "+amount_money+", source_id = "+source_id+"]";
}
}
You need to create object for each like under :
Amount_money am = new Amount_money();
am.setAmount("100");
am.setCurrency("USD");
DataDto dto = new DataDto();
dto.setIdempotency_key("your key");
dto.setsource_id("your id");
dto.setAmount_money(am);
RetrofitInterfaces.IMakePayment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IMakePayment.class);
Call<Void> call = service.listRepos(dataDto);
// yo get the point follow along
Most likely the passed JSON structure is not serialized in the same format.
"amount_money": {
"amount": 100,
"currency": "USD"},
I would at first use for private String amount_money; a real DTO having the amount and currency fields. This should give progress. I'm not 100% sure how the underscore mapping of attributes looks like, but this is the next step.
Add logging to be able to see the passed data. A quick search reveals this tutorial: https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses. When seeing the transmitted data it should be easy to compare the expected and sent data.
Please check your base url.
In your curl you have https://connect.squareupsandbox.com/v2/payments
But in the code you have
private static final String BASE_URL = "https://connect.squareupsandbox.com/v2/payments/";
There is extra / (slash) in the end. I've seen cases where it was the issue. Could be your problem :)

Custom API in Azure APP Serivce examples searched for Android Client

I need a working example for a custom API for Microsoft Azure App Service.
I could not get any useful or working information/examples for that, or they just show each time different approaches which are outdated?!?!
For now I have a working table controller which gets information from database and returns it back to my Android client. Now I need to define a custom API Controller to get a string back. In the examples they are all sending an object to the service in order to get an object back. I do not want to send anything to the API, just retrieve some information back from a GET Request.
Regards
// EDIT - Added / edited client / server code to Post a String.
You can use the following code to do a GET request on the auto generated API controller Visual Studio creates (ValuesController).
private void getStringFromAzure() throws MalformedURLException {
// Create the MobileService Client object and set your backend URL
String yourURL = "https://yourApp.azurewebsites.net/";
MobileServiceClient mClient = new MobileServiceClient(yourURL, this);
// Your query pointing to yourURL/api/values
ListenableFuture<JsonElement> query = mClient.invokeApi("values", null, GetMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
// Since you are on a async task, you need to show the result on the UI thread
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onFailure(Throwable throwable) {
Log.d(TAG, "onFailure: " + throwable.getMessage());
}
});
}
public void sendString(final String someString) throws MalformedURLException {
// Your query pointing to /api/values/{String}
ListenableFuture<JsonElement> query = mClient.invokeApi("values/" + someString, null, PostMethod, null);
// Callback method
Futures.addCallback(query, new FutureCallback<JsonElement>() {
#Override
public void onSuccess(JsonElement jsonElement) {
// You are expecting a String you can just output the result.
final String result = jsonElement.toString();
}
#Override
public void onFailure(Throwable throwable) { }
});
}
The backend API: (ValuesController)
{
// Use the MobileAppController attribute for each ApiController you want to use
// from your mobile clients
[MobileAppController]
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
return "Hello World!";
}
// POST api/values/inputString
public string Post(string inputString)
{
return inputString;
}
}
}
You can also send parameters along in the following way:
List<Pair<String, String>> parameters = new ArrayList<>();
parameters.add(new Pair<>("name", "John"));
parameters.add(new Pair<>("password", "fourwordsalluppercase"));
ListenableFuture<JsonElement> query = client.invokeApi("yourAPI", PostMethod, parameters);
Or as json in the body:
JsonObject body = new JsonObject();
body.addProperty("currentPassword", currentPassword);
body.addProperty("password", password);
body.addProperty("confirmPassword", confirmPassword);
ListenableFuture<JsonElement> query = mClient.invokeApi("yourAPI", body, PostMethod, null);
Based on my understanding, I think there are two parts in your question which include as below. And I think you can separately refer to two sections to get the answers and write your own example.
How to define a custom API on Azure Mobile App to retrieve data from database? Please refer to the section Custom APIs to know how to do with Azure Mobile App backend.
How to call a custom API from Android App? Please refer to the section How to: Call a custom API to know how to do with Android SDK.

Trouble get special character in retrofit 2 and gson

I'm trying to get a json list from a web service.
This is the json string return by server :
[{"categoryName":"Política"},{"categoryName":"Economía"},{"categoryName":"Cultura"},{"categoryName":"Deportes"}
The problem is converting in to the POJO. The special characters (í) it's appear like "Pol�tica".
This is the retrofit call function :
#GET("categories")
public Call<List<CategoryPojo>> getCategorias(#Query("sitename") String site)
this is the callback function:
Call<List<CategoryPojo>> call = restservice.getApiService().getCategorias(medio);
try {
call.enqueue(new Callback<List<CategoryPojo>>() {
#Override
public void onResponse(Call<List<CategoryPojo>> call, Response<List<CategoryPojo>> response) {
List<CategoryPojo> categories = response.body();
if (listener != null)
listener.onDataLoaded(categories);
}
#Override
public void onFailure(Call<List<CategoryPojo>> call, Throwable throwable) {
Log.e("Retrofit Error", throwable.getMessage());
}
});
this is the POJO:
public class CategoryPojo implements Serializable{
public CategoryPojo() { }
#SerializedName("categoryName")
private String name;
public String getName()
{
return this.name;
}
}
The result of the request to the Web services, (output in browser) is :
[{"categoryName":"Política"},{"categoryName":"Economía"},{"categoryName":"Cultura"},{"categoryName":"Deportes"},{"categoryName":"Salud"},{"categoryName":"Ciencia y Tecnología"},{"categoryName":"Medio Ambiente"},{"categoryName":"Medios"},{"categoryName":"Militar e Inteligencia"},{"categoryName":"Sociedad"}]
So, the return json has a good encoding...i think that maybe is about the way retrofit read the response.
I'm using retrofit-2.0.2, gson-2.6.1, converter-gson-2.0.2, okhttp-3.2.0.
Any help? please
You should check Content-type in the response headers. Look for the charset value and try to change that on the backend side to application/josn;charset=UTF-8. That worked for me.

Retrofit returns object with null members

When I try to parse the following JSON with Retrofit, I end up with null member objects.
Parsing:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(CallerInfo.API_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
InGameInfo igi = restAdapter.create(InGameInfo.class);
Game game = igi.fetchInGameInfo("EUW", "sasquatching");
Log.d("Cancantest", "Game " + game); //Not null
Log.d("Cancantest", "Team one " + game.getTeamOne()); //Null
Game Class:
#SerializedName("teamTwo")
#Expose private Team teamTwo;
#SerializedName("teamOne")
#Expose private Team teamOne;
public void setTeamOne(Team teamOne) {
this.teamOne = teamOne;
}
public void setTeamTwo(Team teamTwo) {
this.teamTwo = teamTwo;
}
public Team getTeamOne() {
return teamOne;
}
public Team getTeamTwo() {
return teamTwo;
}
Team Class:
#SerializedName("array")
#Expose private TeamMember[] teamMembers;
public void setTeamMembers(TeamMember[] teamMembers) {
this.teamMembers = teamMembers;
}
public TeamMember[] getTeamMembers() {
return teamMembers;
}
Example JSON:
{
"game":{
"teamTwo":{
"array":[]
},
"teamOne":{
"array":[]
}
}
}
The JSON contains a top level "game" entry so you cannot directly deserialize an instance of game. You need another type which has a field of type Game that represents the response.
public class Response {
public final Game game;
public Response(Game game) {
this.game = game;
}
}
You can put your JSON in a string and use Gson directly to test how the response will be deserialized. This behavior has almost nothing to do with Retrofit and all to do with the behavior of Gson.
String data = "...";
Game game = gson.fromJson(data, Game.class);
Response response = gson.fromJson(data, Response.class);
There can be one more reason for somewhat similar behavior: in this case debugger actually has no field members for the response returned from Retrofit.
And the reason for that is proguard. If you are using minifyEnabled true, make sure you explicitly tell it to keep your POJOs. It can be something like that:
#save model classes
-keep class com.example.app.**.model.** {*; }

#Get arbitrary query parameters in Android Annotations

It seems that I am unable to set arbitrary query parameters to a #Get declaration
My endpoint looks like
http://api.lmiforall.org.uk/api/v1/ashe/estimateHours?soc=2349&coarse=true
There are a non trivial amount of parameters to this query, is there a declaration I can use to indicate this to the #Rest interface?
I tried declaring it as this, but it complains about fields being unused.
#Get("estimateHours")
ASHEFilterInfo GetEstimateHours( int soc, boolean coarse, String filters, String breakdown);
java: #org.androidannotations.annotations.rest.Get annotated method has only url variables in the method parameters
Look at AA cookbook.
Try this (not tested):
#Rest(rootUrl = "http://api.lmiforall.org.uk/api/v1/ashe")
public interface MyService {
#Get("/estimateHours?soc={soc}&coarse={coarse}&breakdown={breakdonw}&filters={filters}")
ASHEFilterInfo GetEstimateHoursFiltered( int soc, boolean coarse, String filters, String breakdown);
#Get("/estimateHours?soc={soc}&coarse={coarse}&breakdown={breakdonw}")
ASHEFilterInfo GetEstimateHours( int soc, boolean coarse, String breakdown);
}
When I needed to create #Get request with many dynamic parameteres, and some of them could be duplicated, I had resolved that problem so:
#Rest(rootUrl = "http://example.com:9080/",
converters = { GsonHttpMessageConverter.class },
interceptors = { ApiInterceptor.class })
public interface ExampleApi {
#Get("content/home/product-type/list?{filters}&domain={domain}") //filters is String like "param1=value1&param1=value2&param3=value3"
ProductTypeListResponse getProductTypeList(int domain, String filters);
}
public class ApiInterceptor implements ClientHttpRequestInterceptor {
private static final String TAG = ApiInterceptor.class.getSimpleName();
#Override
public ClientHttpResponse intercept(final HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
final QueryMultiParamsHttpRequest modifiedRequest = new QueryMultiParamsHttpRequest(request);
return execution.execute(modifiedRequest, body);
}
}
public class QueryMultiParamsHttpRequest implements HttpRequest {
private static final String TAG = QueryParametersBuilder.class.getSimpleName();
private HttpRequest httpRequest;
public QueryMultiParamsHttpRequest(final HttpRequest httpRequest) {
this.httpRequest = httpRequest;
}
#Override
public HttpMethod getMethod() {
return httpRequest.getMethod();
}
#Override
public URI getURI() {
final URI originalURI = httpRequest.getURI();
final String query = originalURI.getQuery() != null ? originalURI.getQuery().replace("%3D", "=").replace("%26", "&") : null;
URI newURI = null;
try {
newURI = new URI(originalURI.getScheme(), originalURI.getUserInfo(), originalURI.getHost(), originalURI.getPort(), originalURI.getPath(),
query, originalURI.getFragment());
} catch (URISyntaxException e) {
Log.e(TAG, "Error while creating URI of QueryMultiParamsHttpRequest", e);
}
return newURI;
}
#Override
public HttpHeaders getHeaders() {
return httpRequest.getHeaders();
}
}
So, I created a wrapper for HttpRequest, that can decode symbols "=" and "&". And this wrapper replaces original HttpRequest in ApiInterceptor. This is a little hacky solution, but it works.
I ran into this same issue and came up with a another solution that while far from ideal, works. The particular problem I was trying to solve was handling "HATEOAS" links.
What I ended up doing was creating a separate class called HATEOASClient to contain endpoint methods that would not escape the HATEOAS links passed in as params. To do that I basically just looked at an auto generated endpoint method and coped/tweaked the body in my implementation.
These methods use the same RestTemplate instance AndroidAnnotations sets up so you still get access to all the general setup you do on the RestTemplate.
For example:
public ResponseEntity<Foo> postFoo(Foo foo) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(RestHeader.AUTH_TOKEN_HEADER, getClient().getHeader(RestHeader.AUTH_TOKEN_HEADER));
httpHeaders.set(RestHeader.ACCEPT_LANGUAGE_HEADER, getClient().getHeader(RestHeader.ACCEPT_LANGUAGE_HEADER));
httpHeaders.setAuthorization(authentication);
HttpEntity<Foo> requestEntity = new HttpEntity<>(null, httpHeaders);
HashMap<String, Object> urlVariables = new HashMap<>();
urlVariables.put("link", foo.getLinks().getFooCreate().getHref());
URI expanded = new UriTemplate(getClient().getRootUrl().
concat(API_VERSION + "{link}")).expand(urlVariables);
final String url;
try {
url = URLDecoder.decode(expanded.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return getClient().getRestTemplate().
exchange(url, HttpMethod.POST, requestEntity, Foo.class, urlVariables);
}
If all parameters is required you can use #Path annotation.
#Rest(rootUrl = "http://api.lmiforall.org.uk/api/v1/ashe")
public interface MyService {
#Get("/estimateHours?soc={soc}&coarse={coarse}&breakdown={breakdown}&filters={filters}")
ASHEFilterInfo GetEstimateHours(#Path int soc, #Path boolean coarse, #Path String breakdown, #Path String filters);
}
If one of the parameters is optional, there isn't yet a solution that can you can easily pass parameters using Android Annotations. But anybody can contribute to better Android Annotations.
if you define the params for each method then you need to provide them in each request. I thought this was sort of over kill too so what I did was just make a generic get/post request in my api client then just manually enter the values, if you don't define the root url I suppose you could use the QueryStringBuilder class and build the uri that way.
#Rest(rootUrl = "https://path/to/api/", converters = { FormHttpMessageConverter.class,
GsonHttpMessageConverter.class, ByteArrayHttpMessageConverter.class })
public interface ApiClient {
#Get("{uri}")
JsonElement apiGet(String uri);
#Post("{uri}")
JsonObject apiPost(String uri,MultiValueMap data);
RestTemplate getRestTemplate();
void setRootUrl(String rootUrl);
void setRestTemplate(RestTemplate restTemplate);
}
Example usage
JsonElement resp = apiClient.apiGet("method/?random_param=1&another_param=test);
It's not as clean but can be dynamic

Categories

Resources