/*
* Licensed to
Q:
How to check i
The Gundam series
The present invent
Introduction {#s1}
Vancouver has a se
Introduction {#S1}
Q:
how do i split
Q:
How do I disab
Q:
How to use .clQ:
What is the right way to do an asynchronous call to fetch a JSON payload using Retrofit 2.0?
I was trying to learn how to use Retrofit for an Android app, but the example they gave at the official site in the following link: https://futurestud.io/tutorials/retrofit-tutorial-part-3-asynchronous-networking does not make sense.
For example, if I'm retrieving some JSON data from a server using retrofit, it should look like this:
@GET("/data.json")
Call dataResponse()
And then, I should implement the callback like this:
dataResponse().enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
Data data = response.body();
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
When you send the request using http://192.168.1.2/getdata.php it will create a request with the following body:
{
"data": {
"name": "data",
"state": "123",
"data": [{
"name": "data",
"state": "123",
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
}
}, {
"name": "data",
"state": "123",
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
}
}]
}
}
I thought that it could be done as follows:
interface ListModel {
String name;
String state;
List data;
}
interface Data {
String name;
String state;
List data;
}
class DataService {
private static final String BASE_URL = "http://192.168.1.2";
private List get(String... params) {
List list = new ArrayList<>();
for(String param : params) {
String uri = String.format("get?%s=%s", param, param);
Call call = HttpClient.getHttp().getCall(uri);
try {
list = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get JSON from API and parse it using GSON
Call> call = new RetrofitInstance(DataService.class)
.get(new String[]{"data", "id", "2"}, new String[]{"name", "state", "data"});
call.enqueue(new Callback>() {
@Override
public void onResponse(Call> call, Response> response) {
if (response.isSuccessful()) {
List models = response.body();
// Do something with models
} else {
// handle error
String error = response.errorBody().string();
}
}
@Override
public void onFailure(Call> call, Throwable t) {
}
});
}
}
class RetrofitInstance {
public static Retrofit getRetrofitInstance() {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
And I was going to parse the JSON in the RetrofitInstance class like this:
JSONObject jsonObject = new JSONObject(jsonString);
Gson gson = new Gson();
DataModel dataModel = gson.fromJson(jsonObject.toString(), DataModel.class);
List dataList = dataModel.getData();
But, in this case, the JSON is not properly parsed. The JSON that is sent from the server is different from what I'm expecting. It looks like this:
{
"data": {
"name": "data",
"state": "123",
"data": [{
"name": "data",
"state": "123",
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
}
}],
"data": {
"name": "data",
"state": "123",
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
}
},
"data": {
"name": "data",
"state": "123",
"data": {
"name": "data",
"state": "123"
},
"data": {
"name": "data",
"state": "123"
},