Retrofit - Different API responses on different devices - java

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.

Related

Retrofit custom timeout is not working

I am using retrofit version 2.3.0 and OkHttp version 3.8.1. I am facing a problem that I cannot change timeout for my requests. I was searching for answer in google and there is only one solution - Change timeouts in OkHttpClient and set it to retrofit builder, but it is not working :/. here is a code that I use for changing timeouts:
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson));
builder.client(new OkHttpClient.Builder()
.connectTimeout(50000, TimeUnit.MILLISECONDS)
.readTimeout(50000, TimeUnit.MILLISECONDS).build());
retrofit = builder.build();
But after this requests still lasts for 10 seconds and I get SocketTimoutException
EDIT
I have done some more research and I was testing it with different versions of Retrofit. I stoped on retrofit version 2.0.1 and this version also not allows me to change timeout :/. Have someone got any idea what to do now?
EDIT 2
I have this problem now in two places. I will describe the use case so maybe it will help with finding the solution.
use 1:
I want to download file from remote server. I call web service and I wait for response with file stream. I need to wait longer because web service needs to get file from database and put it into hard drive (there is no possibility to change it). When file is big copying it to hard drive takes time and application receives SocketTimeoutException.
use 2:
I upload file to the server (and it works fine). After upload I need to let web service know that it should load file into the database. I send the request and situation is similar to the use 1, because when file is big, loading takes much time and I get SocketTimeoutException.
UPDATE
I would like to clear up situation. The problem was occurring because I had the second OkHttpClient assigned to the Retrofit which was overriding timeout. If someone will have a similar problem I recommend to check if there is a similar situation to the one I was facing (second OkHttpClient that overrides timeout).
Did you try setting the .writeTimeout on the client:
.writeTimeout(50, TimeUnit.SECONDS)
Also while testing you can pass the timeout duration to 0 if you want the request to not timeout.
And also, the request would fail if there is any connectivity problem or server is not available. Try sending the same request using Postman.

Get data from FIX protocol in mobile apps

I need to create an app which get's it's displayed data from FIX protocol in real time.
How would be best practice to accomplish this?
Should the mobile app read directly the FIX data?
Should I put a server in between to transform the FIX data to JSON or something more prepared for service consumption? And if so... how would I do it to not lose the real time? Creating a websocket in Java which for the mobile apps to call and that websocket would transform the FIX data to JSON that way?
I never worked with the FIX protocol before by the way.
So like, the question is, can you put a FIX client on a mobile phone? I guess you can, and I am sure it will happen soon.
Yes, anything can read FIX data, it's simply text stream name value pairs, where the name is a numbered tag to identify some particular financial data or process, as agreed between the FIX community as a whole, or between direct counterparties using messaging data dictionaries.
These days you could try using SBE throughout, including at the mobile client, but for now current practise is yes, to use JSON for client endpoints (where the JSON name is not a FIX tag but the underlying FIX field name so it's easier for people to understand) and pass JSON messages to the mobile device in some kind of REST data processing.
So yeah, you'd have a FIX server (if you are new to FIX try QuickFix) to connect with your provider. Then what you need between that server and mobile clients is asynchronous messaging. Consider a quote request is not a simple request / response where you can block the request thread until a single response is received, it's a request followed by a stream of many responses. So you want an event handler style to route the responses back to clients asynchronously. With many mobile UI clients making requests and receiving responses you need to demultiplex the responses from the 1 FIX server out to the many mobile clients. That's something that QuickFIX itself kind of already does using a session layer. You need to do the same using sessions or identifiers in the messages you're passing around.
Yeah I guess websockets are intended to be that solution for real time. You could write a native mobile app though, using something like node or react to handle the message events.

Jsoup, Reddit, OAuth2, and 429 HTTP Errors

So I'm trying to write an executable JAR for a small subreddit I run.
I have a post that Jsoup connects to and reads all the URLs on that page. In another method, it then connects to all those URLs (that are just comments on the post) and gets the HTML from the comments and saves them to a HashMap.
This is great however I am getting a 429 HTTP Error. So to resolve this, I added a short 5 second wait. Now I'm getting a SocketTimeoutException "Read timed out". Once I lowered the time down to 3 seconds, I was bouncing between the two.
Now I run a few Reddit bots with Python and I'm able to make a lot more requests than what I'm doing here. I actually have a single bot that makes thousands of requests every minute. So I know it's possible to make these requests.
My question essentially is, how am I able to make multiple requests to Reddit and avoid the 429 HTTP Error? I'm using Jsoup to connect and read the HTML.
While I'm sure connecting to Reddit via. their OAuth2 API will fix the issues, I have no idea how to actually use OAuth2 in Java (I actually use a wrapper in Python so it's fair to say I don't know at all) and I don't know how to then use that with Jsoup.
My question essentially is, how am I able to make multiple requests to Reddit and avoid the 429 HTTP Error?
You answer this yourself:
While I'm sure connecting to Reddit via. their OAuth2 API will fix the issues,
As specified in the API documentation, you get twice as many requests per second if authenticated using OAuth.
Have you looked around for examples on how to handle OAuth flows in Java?
You might also find it easier to use one of the wrapper libraries for Java, instead of handling all this yourself.
Just set header and you can easily pass it
User-Agent: super happy flair bot by /u/spladug

Simulate form post using http client in Android app?

So, I'm currently developing an app for a service which has a json-based (unfortunately) read only API. Retrieving content is no problem at all, however the only way to post content is using a form on their site which location is a PHP script. The service is open source so I know which fields the form expects, but whatever I send, it always results in a BAD REQUEST.
I captured the network traffic inside my browser and as far as I can see, the browser constructs a multipart form request, however when I copy the request and invoke it again using a REST client, a BAD REQUEST gets returned.
Is there a way to construct a http request in Android that simulates a form post?
If it's readonly I think you wouldn't be able to make requests with POST (it's assume for editing or adding things).
If you let me make you an advise, I recommend you using this project as a Library.
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It makes you easy deal with apis, control network problems etc.
You can find a sample of use there.
You only have to:
Build your URL
Tell the component to execute in POST mode
Build your JSON
As I told you, I don't know even if it will work.
I hope it helps!!!

How to check if new JSON file is available?

I am building a mobile application that get all the information from a Java backend. The data is available through REST in JSON format. How would you manage to discover on the mobile device if the JSON is new or not? I was thinking of having one really simple JSON format file with only a timestamp that the mobile device checked each time to compare to what already stored...but it doesn't seems to be a good solution, at least I think. So are there other ways to do this? I am developing both the backend Java server application and the mobile device app (iOS).
Use HTTP Headers including the HTTP ETag. Here's an example in Java.
If-Modified-Since
Allows a 304 Not Modified to be returned if content is unchanged
If-None-Match
Allows a 304 Not Modified to be returned if content is unchanged

Categories

Resources