I wanted to display a text from a JSON
value. I'm using Retrofit
to make API
call I don't know if I'm doing it right. Anyway here's my code.
Here's the url link: http://api.icndb.com/jokes/random .
The website will display a random joke each time. Here's the example of an output from the website:
{ "type": "success", "value": { "id": 175, "joke": "When Chuck Norris was a baby, he didn't suck his mother's breast. His mother served him whiskey, straight out of the bottle.", "categories": [] } }
fragment.java
String url = "http://api.icndb.com/";
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
RetroFitHelper client = retrofit.create(RetroFitHelper.class);
Call<Api> call = client.findJoke("random");
call.enqueue(new Callback<Api>() {
@Override
public void onResponse(Response<Api> response, Retrofit retrofit) {
String result = response.body().getJoke();
Toast.makeText(getContext()," The word is: " + result ,Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(getContext()," Error..." ,Toast.LENGTH_LONG).show();
}
});
RetroFitHelper.java
public interface RetroFitHelper {
@GET("/jokes/{random}")
Call<Api> findJoke(@Path("random") String random);
}
Model class
public class Api {
@SerializedName("joke")
private String joke;
public String getJoke() {
return joke;
}
public void setJoke(String joke){
this.joke = joke;
}
}
Their is a problem in your model class which you are using.
The response of api is :
So for getting value of joke from response as a string your model class should be like this :
and your Value Class is :
And now you can get your value like this :
Do changes as per you requirement.
Happy Coding..
The provided json response has a json object named
value
inside another json object(It is of the form{..,"value":{}}
). So we need two model classes - one for theouter json object
and another one for theinner json object
(value).You will need to have two model classes like this
and the following one for value object
Now,
response.body()
will have the result ofouter json object(Api)
andresponse.body().getValue()
will have the result forinner json object(Value)
.Now in your response callback, get response like this
<uses-permission android:name="android.permission.INTERNET" />
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
As @Aswin suggest and @Navneet answered you have problem in your POJO class. I suggest you to use jsonschema2pojo or RoboPOJOGenerator so next time you avoid to stuck this kind of error.
Steps
1) Go to http://www.jsonschema2pojo.org/
2) Paste your response over there and enter package and class name
3) Choose target language as Java or Kotlin(if you are using)
4) Source type as Json
5) Annotation style as Gson
6) Click Preview
7) Copy and paste those classes to your app package
Add this permission in your
AndroidManifest.xml
Add this in your
build.gradle (Module:app)
Add this interface ,
Create 2 Model Class :
com.example.app.Api.java
Create Value Model Class :
com.example.app.Value.java
Add this code in your Activity
onCreate()
Method