为什么Retrofit响应为字段赋予空值,而响应可以获取对象?

时间:2017-11-14 13:16:54

标签: android retrofit2

我只是在Android Studio 2.3中创建一个简单的应用程序来从Web应用程序获取JSON响应。我选择了Retrofit 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));

返回null

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();

}

返回服务的类 - GrocsService

package com.makemyapp.firstapplication;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class GrocsService {

    public  static GrocsApi service;
    public static String BASE_URL = "http://10.0.2.2:8000/";


    public static GrocsApi getGrocsService(){
        if (service==null){

            Retrofit retrofit= new Retrofit.Builder()
                              .baseUrl(BASE_URL)
                              .addConverterFactory(GsonConverterFactory.create())
                              .build();
            service = retrofit.create(GrocsApi.class);

            return service;
        }

return  service;
    }

}

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());

                  }
              });


            }

        }
    );
}
}

这是我的POJO课程 - 项目

package com.makemyapp.firstapplication;


public class Item {

    private Integer itemId;
    private Object itemName;
    private Object itemDescription;
    private Object itemPhotoUrl;
    private Object itemWikipediaLink;
    private Object itemTag;

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public Object getItemName() {
        return itemName;
    }

    public void setItemName(Object itemName) {
        this.itemName = itemName;
    }

    public Object getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(Object itemDescription) {
        this.itemDescription = itemDescription;
    }

    public Object getItemPhotoUrl() {
        return itemPhotoUrl;
    }

    public void setItemPhotoUrl(Object itemPhotoUrl) {
        this.itemPhotoUrl = itemPhotoUrl;
    }

    public Object getItemWikipediaLink() {
        return itemWikipediaLink;
    }

    public void setItemWikipediaLink(Object itemWikipediaLink) {
        this.itemWikipediaLink = itemWikipediaLink;
    }

    public Object getItemTag() {
        return itemTag;
    }

    public void setItemTag(Object itemTag) {
        this.itemTag = itemTag;
    }

}

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}]

有没有人能解决我的问题?

2 个答案:

答案 0 :(得分:2)

只需在Item类的每个字段上添加@SerializedName,其中JsonObject名称将填充此字段。

例如:

@SerializedName("ItemId") // Case sensitive
private Integer itemId;

MainActivity

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.

比你的onResponse回调中,你得到了你的清单:

List<item> myResponse =  (List<Item>) response.body();

而不是你获取你的物品清单。

答案 1 :(得分:1)

您的变量是:“itemDescription”,但您的json响应是“ItemDescription”,您应该认为解析器区分大小写。