Android 为什么改装响应为字段提供空值,而响应可以获取对象?

Android 为什么改装响应为字段提供空值,而响应可以获取对象?,android,retrofit2,Android,Retrofit2,我只是在AndroidStudio 2.3中制作一个简单的应用程序,从Web应用程序获取JSON响应。我选择改装2.0来使用响应 似乎我可以在onResponse Object item = response.body().get(1); Log.e("success", String.valueOf(item)); 在logcat内部,它取得了成功 success:com.makemyapp.firstapplication.Item@67205ae 但是当试图获取对象内部的字段时 O

我只是在AndroidStudio 2.3中制作一个简单的应用程序,从Web应用程序获取JSON响应。我选择改装2.0来使用响应


似乎我可以在
onResponse

Object item =  response.body().get(1);
Log.e("success", String.valueOf(item));
在logcat内部,它取得了成功

success:com.makemyapp.firstapplication.Item@67205ae
但是当试图获取对象内部的字段时

Object item =  response.body().get(1).getItemDescription();
Log.e("success", String.valueOf(item));
它返回空值

success: null

这是接口-GrocsApi

package com.makemyapp.firstapplication;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;

public interface GrocsApi {

    @GET("api/getitemlist")
    Call<List<Item>> getItem();

}
MainActivity.java,其中启动请求调用

package com.makemyapp.firstapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MainActivity extends AppCompatActivity {


    Button button;
    //TextView textView; for later use


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onClickGetData();
    }


    public void onClickGetData(){

      //  textView=(TextView)findViewById(R.id.textView); for later use
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
  Call<List<Item>> call =   GrocsService.getGrocsService().getItem();

           call.enqueue(new Callback<List<Item>>  () {
                  @Override
                  public void onResponse(Call<List<Item>>  call, Response<List<Item>>   response) {

 if(response.isSuccessful()) {
     Object item =  response.body().get(1).getItemDescription();

     Log.e("success", String.valueOf(item));

 } else{
     Log.e("response is not good", "");
 }
                  }

                  @Override
                  public void onFailure(Call<List<Item>>   call, Throwable t) {

                      Log.e("failed", t.getMessage());

                  }
              });


            }

        }
    );
}
}
JSON响应是

[{"ItemId":1,"ItemName":"Orange","ItemDescription":"Sweet and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Orange_(fruit)","ItemWikipediaLink":"'https:\/\/en.wikipedia.org\/wiki\/Orange_(fruit)#\/media\/File:Orange-Whole-%26-Split.jpg","ItemTag":"RS.70 per kg"},{"ItemId":3,"ItemName":"Apple","ItemDescription":"Reddish","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Apple","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Apple#\/media\/File:Red_Apple.jpg","ItemTag":"RS.10 per kg"},{"ItemId":4,"ItemName":"Mango","ItemDescription":"Fresh","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Mango#\/media\/File:Mangoes_pic.jpg","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Mango","ItemTag":"RS.100 per kg"},{"ItemId":5,"ItemName":"Pineapple","ItemDescription":"Large and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Pineapple#\/media\/File:Pineapple_and_cross_section.jpg","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Pineapple","ItemTag":"RS.50 per kg"},{"ItemId":6,"ItemName":"Grapes","ItemDescription":"Dark and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Grape#\/media\/File:Abhar-iran.JPG","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Grape","ItemTag":"110 per kg"},{"ItemId":7,"ItemName":"Guava","ItemDescription":"Green","ItemPhotoUrl":null,"ItemWikipediaLink":null,"ItemTag":"50 per kg"},{"ItemId":8,"ItemName":null,"ItemDescription":null,"ItemPhotoUrl":null,"ItemWikipediaLink":null,"ItemTag":null}]

有人能指出我的问题吗?

只要在Item类的每个字段上添加
@SerializedName
,其中包含将填充此字段的
JsonObject
名称即可

例如:

@SerializedName("ItemId") // Case sensitive
private Integer itemId;
在您的
main活动中

Call<List<Item>> call =   GrocsService.getGrocsService().getItem(); //It's here where you call getItem(). you will enque the request after that as you already did.

然后获取项目列表。

变量是:“itemDescription”,但json响应是“itemDescription”“你应该考虑解析器是区分大小写的。

你在模型的字段中漏掉了@ SerializedName。请添加COM.MAKEMYAPP.FrestAptudio.item类代码,也许它是简单的序列化错误,指定项目中的字段的精确类型(在您的情况下为字符串),并添加@ SerialZeDeNeND(“ItEdTrimes”)。字段的注释declaration@Fakher@Viktor Yakunin我使用的变量名与Json中的相同。那么,有必要序列化吗?这是创建POJO类的简单方法:您能帮助我在单个textView中获取每个对象中每个变量的内容吗?如果您的响应只是一个对象(而不是列表),您可以很容易地做到:
Item=response.body();yourTextView.setText(item.getItemName)我的响应是一个对象列表!我也有疑问。实际上,CallgetItem()的定义在哪里它是否在匿名类中?我理解这一点,在您更新的答案中,我认为没有必要将response.body()强制转换为列表。因为response.body()已经是一个项目列表。
@SerializedName("ItemId") // Case sensitive
private Integer itemId;
Call<List<Item>> call =   GrocsService.getGrocsService().getItem(); //It's here where you call getItem(). you will enque the request after that as you already did.
List<item> myResponse =  (List<Item>) response.body();