gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:14:55 27 4
gpt4 key购买 nike

我只是在 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));

它返回一个空
success: null

这是接口(interface)- 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();

}

退货服务类- 杂货服务
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 类(class)- 项目
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}]

有人能发现我的问题吗?

最佳答案

只需添加 @SerializedName在 Item 类的每个字段上使用 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 :
List<item> myResponse =  (List<Item>) response.body();

而不是你获取你的项目列表。

关于android - 为什么改造响应为字段提供空值,而响应可以获取对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47286865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com