extract and display names and class using retrofit2. 3.I tried several sites, but only able to fetch data directly from an array 4.But not from an array of objects. That would be formed by this code.
{ "Class1" :{ "A": [ { "name" : "paul", "class" : "first" } ], "B": [ { "name" : "anna", "class" : "second" } ] }, "Class2" :{ "A": [ { "name" : "matthew", "class" : "first" } ], "B": [ { "name" : "joe", "class" : "second" } ] } }
Can you tell me Why I am getting into onFailure() method. What is wrong with my code
public class MainActivity extends AppCompatActivity {
private ListView listView;
private static final String Tag = "com.example";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.bList);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
String API_BASE_URL = "xyz.com";
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
ApiInterface client = retrofit.create(ApiInterface.class);
Call<List<Example>> call = client.getdate();
call.enqueue(new Callback<List<Example>>() {
@Override
public void onFailure(Call<List<Example>> call, Throwable t) {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
if (response.isSuccessful()) {
//Response success. Handle data here
}
else{
//For getting error message
Log.d("Error message",response.message());
//For getting error code. Code is integer value like 200,404 etc
Log.d("Error code",String.valueOf(response.code()));
}
}
});
}
}
Example is my main class
The following is MyAPIInterface.java
public interface ApiInterface {public interface ApiInterface {
@GET("/sement")
Call<List<Example>> getdate(@Query("date")String date);
}
Your JSON is invalid. But, for your question, the answer is You have to create models using http://www.jsonschema2pojo.org/ from JSON and after that use GSON library to map json data to models.
Refer this link you will get the idea https://guides.codepath.com/android/Consuming-APIs-with-Retrofit
OR
For above example, you will get the name & class like
List<A> alist = mainclass.getClass1().getA();
for (int i = 0; i < alist.size(); i++) {
String name = alist.get(i).getName();
String class = alist.get(i).getClass();
}
And do same for Class B
First Validate your Json.
Use this tool for Json to Class Objects http://www.jsonschema2pojo.org/
Once you create Objects.
here getData() is first json object.
List<A> listA= response.body().getData().getClass1().getA();
List<B> listB=response.body().getData().getClass1().getB();
for(A list:listA)
list.getname(); //paul
list.getclass(); //first
for(B list:listB)
list.getname(); // anna
list.getclass(); //second
List<A> listA= response.body().getData().getClass2().getA();
List<B> listB=response.body().getData().getClass2).getB();
for(A list:listA)
list.getname(); //matthew
list.getclass(); //first
for(B list:listB)
list.getname(); //joe
list.getclass(); //second
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.