• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Is it wise to use Retrofit for Java application?

 
Ranch Hand
Posts: 43
Notepad Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a dilemma here. Earlier I used to use Core Java Library HttpUrlConnection to get JSON data over the web and use to parse it with GSON. Earlier these days my university teacher told me to use Retrofit library instead. Thinking of it a better and optimized way to code, I started working with it.

I loved its simplicity and rich feature but here I am struggling a lot due to of Callback anonymous inner class. Due to of it I am unable to pass the objects to other methods of mail application files, which I used to handle easily when I worked with HttpUrlConnection. Now here is my question, am I going any wrong, or is it really not a wise decision to use Retrofit with my java applications and I should stick to HttpUrlConnection.

Please let me know!

I am pasting a code below just to explain my point here:
 
Saloon Keeper
Posts: 7161
165
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't say whether it's wise to use this library - I know little about it, and it's unclear why your instructor recommended it.

What I can say is that for one project I've recently switched from Java's built-in HTTP library to https://square.github.io/okhttp/, which has a much nicer API, IMO. It's so nice to work with that it's going to be my default HTTP client library going forward, though.
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not this?



Or does that "enqueue" method actually cause something to run asynchronously and call the callback method at an unpredictable future time? I'm not familiar with Retrofit either but I wouldn't expect asynchronous behaviour there.
 
tushar attar
Ranch Hand
Posts: 43
Notepad Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anonymous inner class can only access final or effectively final, hence the reference 'result' cannot be updated. The above code would not compile.
 
Paul Clapham
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure you're right. (Lame excuse: it's hard for me to write some code which I can play with because there's too many other things to set up.)

My next idea was that the class that the getProductById method is part of could have a setProduct(Product) method which would be called in the anonymous class. Maybe that might work -- but your feeling that you would have to change your programming a lot, I'm pretty sure that's right as well.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could consider using some kind of a data structure (Queue?) inside the method getProductId method but outside the inner class, and add the data you get from the callback to it, then use some polling (or similar) to send the data from the method.

This is just an example way of doing that, but need to be analysed and properly designed depending on your application flow/model.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little late to the party, but just to put another opinion here, and point out a flaw.

You have written your method in a synchronous manner, but are making an asynchronous call to the API.

Is there any reason you have to make this call asynchronously (call.enqueue) vs synchronously(call.execute)?  Something like this (untested, but following the API at http://square.github.io/retrofit/2.x/retrofit/)




If you want to make an asynchronous call then you are going to have to change the getProductById method not to return the value directly...
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic