Java 用空字段改装get对象
我使用改型2.4并尝试从Asp.NETCore2.0WebAPI服务获取数据 这里是Java类:Java 用空字段改装get对象,java,c#,android,asp.net-core,retrofit2,Java,C#,Android,Asp.net Core,Retrofit2,我使用改型2.4并尝试从Asp.NETCore2.0WebAPI服务获取数据 这里是Java类: public class Category { private int CategoryID; private String Name; private String Image; public Category(){ Name=""; Image=""; } public Category(int categoryID, String name, String image) {
public class Category {
private int CategoryID;
private String Name;
private String Image;
public Category(){
Name="";
Image="";
}
public Category(int categoryID, String name, String image) {
Name = name;
Image = image;
CategoryID=categoryID;
}
public int getCategoryID() {return CategoryID;}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
}
此处为改装代码:
public class Common {
public static User CURRENT_USER;
public static String SERVER_NAME="http://ip_address:5000";
public static IApiService ApiService;
public Common()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_NAME)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService = retrofit.create(IApiService.class);
}
}
所以,我通过Swagger检查服务器代码,它运行良好:我从类别列表中获取所有数据。
但是,当我试图通过改型从Android代码中获取数据时,我得到的集合是空对象:所有字段都是null或空的(我认为这是默认值)
下面是代码:
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
List<Category> _categoryList =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//some code
Common.ApiService.GetCategoryColl().enqueue(new Callback<List<Category>>() {
@Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
Log.i("GetCategories",response.message());
_categoryList=response.body();
// !!!! HERE. _category list contains objects but all of them
// are empty!
}
@Override
public void onFailure(Call<List<Category>> call, Throwable t) {
Log.e("GetCategories",t.getMessage());
}
});
}
}
公共类主页扩展了AppCompative活动
实现NavigationView.OnNavigationItemSelectedListener{
List _categoryList=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//一些代码
Common.ApiService.GetCategoryColl().enqueue(新回调()函数){
@凌驾
公共void onResponse(调用、响应){
Log.i(“GetCategories”,response.message());
_categoryList=response.body();
//!!!!此处。\类别列表包含对象,但所有对象
//都是空的!
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“GetCategories”,t.getMessage());
}
});
}
}
所以,我不知道,为什么会这样?如何解决这个问题?
谢谢大家! 您尚未将
@SerializedName(“json密钥名”)
添加到Java类别中的字段中:
@SerializedName("categoryId")
private int CategoryID;
@SerializedName("name")
private String Name;
@SerializedName("image")
private String Image;
现在,GSON可以正确地将JSON响应映射到POJO。您还没有将@SerializedName(“JSON键名”)
添加到Java类别中的字段中:
@SerializedName("categoryId")
private int CategoryID;
@SerializedName("name")
private String Name;
@SerializedName("image")
private String Image;
现在,GSON可以正确地将JSON响应映射到POJO。默认情况下,GSON希望字段名与JSON字段名相同,如果要更改此行为,您有两个选项:
1.对于您的情况,请使用下面的示例进行改装:
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_NAME)
.addConverterFactory(GsonConverterFactory.create())
.build();
二,。在java字段上使用注释。默认情况下,Gson希望字段名与Json字段名相同,如果要更改此行为,您有两个选项:
1.对于您的情况,请使用下面的示例进行改装:
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_NAME)
.addConverterFactory(GsonConverterFactory.create())
.build();
二,。在java字段上使用注释。请在ASP.Net项目中发布类别代码。@Hicham,我这样做了。可能的问题是您没有在PoJo变量上提供SerializedName
注释。SerializedName
将是JSON对象的键。请在ASP.Net项目中发布类别代码。@Hicham,我会这样做。可能的问题是您没有在PoJo变量上给出SerializedName
注释。SerializedName
将成为JSON对象的密钥。如果这对您有帮助,请将其标记为答案,谢谢:)如果这对您有帮助,请将其标记为答案,谢谢:)
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SERVER_NAME)
.addConverterFactory(GsonConverterFactory.create())
.build();