'how to convert json(list) to java class
i used Spring boot, retofit i want to get response to user class
response from server is here
{
"userid": "jin",
"sex": true,
"height": 180,
"authorityDtoSet": [
{
"authorityName": "ROLE_USER"
}
]
}
i don't know how to conver authorityName to java class and get response
android code here
public class RetrofitClient {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
public RetrofitService retrofitService = retrofit.create(RetrofitService.class);
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RetrofitClient retrofitClient = new RetrofitClient();
Call<User> call = retrofitClient.retrofitService.getProduct3("jin", "Token");
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccessful()){
Log.i("Tag", "Success : " + response.toString());
Log.i("Tag", "Success : " + response.body().toString());
} else {
Log.i("Tag", "Failed");
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.d("Tag", "Failed" + t.getMessage());
}
});
}
public interface RetrofitService {
@GET("user/{userid}")
Call<User> getProduct3(@Path("userid") String userid, @Header("Authorization") String token);
}
android user class
public class User {
@SerializedName("userid")
private String userid;
@SerializedName("sex")
private boolean sex;
@SerializedName("height")
private int height;
@SerializedName("authorityDtoSet")
private List<AuthorityDto> authorityDtoSet;
@Override
public String toString() {
return "PostResult{" +
"userid=" + userid +
", sex=" + sex +
", height='" + height + '\'' +
", authorityDtoSet='" + authorityDtoSet + '\'' +
'}';
}
}
public class AuthorityDto {
AuthorityDto(){}
private String authorityName;
}
i think User class(android) doesn't match response(Server) so i can't get data how can i get json list data from server? plz help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
