I am creating an android app that requires an api call, the api is still in development, but I want to create a dummy retrofit api request that will return an actual json value as response so I can speed up the app development. But now I don't know how to go about it, I have searched online for answers but I don't seems to understand any.
There are a couple of ways to do this.
Using Retrofit/Okhttp mock, you can place the json file in the assets folder and configure retrofit mock/ okhttp mock to serve the json file as the response, when a particular endpoint is hit.
Retrofit Mock [Runs within your app]
https://github.com/square/retrofit/tree/master/retrofit-mock
OKHttp Mock [Runs within your app]
https://github.com/square/okhttp/tree/master/mockwebserver
Json-server [Runs on your PC/Mac]
https://github.com/typicode/json-server
Related
I have to modify java based old project(servlet , Gradle project) which was not integrated with any of Java framework. For a recent project integration requirement, needs to call a external Api' PATCH request and change some value(owner ID) time to time on that external api hosted web application.
Endpoint looks like following
https://reverinapi/privivo/api/deys#/v1/drive/maks/{id}
Need to change owner id time to time and JSON should following,
{ "meta": { "ownerId": "smtip|appownid1" } }
I tried following way,
com.google.gson.JsonObject mainObject=new com.google.gson.JsonObject();
com.google.gson.JsonObject meta=new com.google.gson.JsonObject();
meta.addProperty("ownerId", "smtip|appownid1");
mainObject.add("meta", meta);
I don't familiar with how to call the api endpoint and please let me know if there any other efficient way to do this api call and change the value.
You need to use some HTTP client library to make the request. There are likely many available for Java, but Apache's is one.
Ah, I also just learnt that as of Java 11, there's an HTTP client included: https://www.baeldung.com/java-9-http-client.
I am having trouble on how to Mock an external API that an internal API calls.
What I want is basically to test the call on the internal API, to see if it reaches the point of the external API path. I am using Mockito with java and can't seem to get it working. I want to intercept the External API call and return a mock response so the external api isn't hit.
Here is what I have tried:
I am using Micronaut using HttpClients in my tests to do something like:
HttpResponse<blah> response = client.toBlocking().exchange(request, blah.class);
, which is the internal API call. The external api call is in the form of:
HttpResponse<blah> response = client.toBlocking().exchange(request, blah.class, error.class);
I am trying to catch the external API call in my Integration test class by:
when(client.toBlocking().exchange(any(MutableHttpRequest.class), any(Argument.class), any(Argument.class))).thenReturn(resp);
But it seems like I cannot even reach the internal api, the call never seems to be made. I believe that it is being intercepted earlier.
Any help or suggestions are appreciated - I am not sure if this is the right approach or if there's some easier way, but thank you in advance.
The problem exactly like the following
I have one api which can provide some data and one apk,
I got my api response with my device and another different old response on the emulator with the same apk!
The emulator response is up to date with the latest changes while the other different response is before the changes.
The app uses retrofit without caching.
So what could be the problem?
Request contains only one header (access token)
Response contains headers like (ETag, Access-Control-Max-Age) but retrofit should not deal with them as there is no cache at all.
Screen shot from postman :
Also when waiting for a while all devices become up to date!
Are you sure with this?
The app uses retrofit without caching
Based on this https://github.com/square/retrofit/issues/678, I think that retrofit (OkHttp inside retrofit) caching your query.
You can change this behavior by 2 ways:
add ?_t=TIMESTAMP into your app query url
remove 'Access-Control-Max-Age' header from your server response
Or you can create new OkHttp client to retrofit and write something like that:
new OkHttpClient.Builder().cache(null).build();
or
Request.cacheControl(CacheControl.FORCE_NETWORK)
So what could be the problem?
If you dont' find the problem on the client, you should look for it on the server side. Any kind of caching (on the database, or on the HTTP response level) can show this behavior.
Without further knowledge about your setup and your test environment it is impossible to figure out what the problem could be. You write
Also when waiting for a while all devices become up to date
What does that mean? How do you determine, if your devices are up to date?
In your complicated case, with backend involved, you should provide an example project on Github where this behavior can be reproduced.
I'm making an app where I want to store data the users input on a google spreadsheet. I have tried Secure HTTP Post in Android already and the first answer does not work when I put it in android studio. Does anyone know another way to send data from an app to the spreadsheet?
use retrofit http://square.github.io/retrofit/
Setup a post method like this
#POST("users/data")
Call<Data> storeData(#Body Data data);
Then make the post call by calling the method with correct data.
All http methods are implemented in similar fashion.
Refer docs for detailed information about how to use retrofit.
I am doing image uploads as outlined in the documentation for the App Engine Images API, using getServingUrl() to generate an upload url, and then forwarding the request to my own handler. I am wondering if there is a good way of unit testing the image handling logic in my handler. If I instantiate my handler in a unit test and pass on a request to it, it fails with a "Must be called from a blob upload callback request". Can I somehow mock it?
Write a mock object that returns a known value from the getServingUrl method and then make sure that your class correctly reads that value and uses it to make the upload request. You have to assume that the GAE code is going to do the right thing in this case. The only logic you should concern yourself with is that which reads that url from GAE and subsequently posts to it with your blob data.