The response of the call from WebService is as follows:
{
"mobilenumber": "09999999999",
"service": "1" ,
"id": "1"
}
How do I parse received Json into objects?
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (response.isSuccessful()) {
}
}
This code gives you how to parse the json.
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (response.isSuccessful()) {
JSONObject jsonobject = new JSONObject(yourresponse);
String mobilenumber = jsonobject.getString("mobilenumber");
String service = jsonobject.getString("service");
String id = jsonobject.getString("id");
}
By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type.
A Converter which uses Gson for serialization to and from JSON. A default Gson instance will be created or one can be configured and passed to the GsonConverterFactory to further control the serialization.
Add to gradle;
compile 'com.squareup.retrofit2:converter-gson:latest.version'
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.xxx.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Assuming you have a Login
model like this:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Login {
@SerializedName("mobilenumber")
@Expose
private String mobilenumber;
@SerializedName("service")
@Expose
private String service;
@SerializedName("id")
@Expose
private String id;
public String getMobilenumber() {
return mobilenumber;
}
public void setMobilenumber(String mobilenumber) {
this.mobilenumber = mobilenumber;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
In your retrofit onResponse()
method:
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (response.isSuccessful()) {
Login loginObject = response.body();
String mobileNumber = loginObject.getMobilenumber();
String service = loginObject.getService();
String id = loginObject.getId();
}
}
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.