作为情侣日R& D,我发现一些解决方案可以做到这一点,但是我无法通过改装2.3版本显示JSON API的任何响应。我知道这个问题可能会重复,但是,任何人都可以帮助我。这就是JSON API的样子
{
"GetInsertPageInfoResponse": {
"Request": {
"IsValid": "True",
"GetInsertPageInfoRequest": null
},
"GetInsertPageInfo": {
"1": [
{
"cat_name": "test1",
"image_thumb_path": "insertPage/thumb/taputapu",
"image_original_path": "upload/insertPage/original/book",
"cat_icon_path": "upload/insertPage/cat_icon/icon.png",
"cat_version": "8",
"item_id": "1",
"item_name": "01.jpg"
}
],
2: [
{
"cat_name": "test2",
"image_thumb_path": "upload/insertPage/thumb/taputapu",
"image_original_path": "photobook/upload/insertPage/original/book",
"cat_icon_path": "upload/insertPage/cat_icon/icon.png",
"cat_version": "6",
"item_id": "3",
"item_name": "08.jpg"
}
]
}
}}
这是我试图解析json,但它仍然无法正常工作
public class APIClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这就是我获得服务的方式
public interface APIService {
@GET("?Service=Core&Operation=GetInsertPageInfo")
Call<GetInsertPageInfoResponse> getInsertPageInfoCall();
}
public class APIUtils {
public static APIService getSOService() {
return APIClient.getClient(API_ROOT_URL).create(APIService.class);
}
}
我尝试使用此POJO
public class GetInsertPageInfoResponse {
@SerializedName("GetInsertPageInfoResponse")
@Expose
private GetInsertPageInfo getInsertPageInfo;
}
public class GetInsertPageInfo {
@SerializedName("GetInsertPageInfo")
@Expose
private Map<String, List<AsobieItem>> mPageInfo;
public Map<String, List<AsobieItem>> getmPageInfo() {
return mPageInfo;
}
public void setmPageInfo(Map<String, List<AsobieItem>> mPageInfo) {
this.mPageInfo = mPageInfo;
}
}
public class AsobieItem {
@SerializedName("cat_name")
@Expose
private String cateName;
@SerializedName("image_thumb_path")
@Expose
private String imageThumbPath;
@SerializedName("image_original_path")
@Expose
private String imageOriginalPath;
@SerializedName("cat_icon_path")
@Expose
private String catIconPath;
@SerializedName("cat_version")
@Expose
private String catVersion;
@SerializedName("item_id")
@Expose
private String itemId;
@SerializedName("item_name")
@Expose
private String itemName;
}
UPDATE !! 这就是我在Activity类中使用retrofit 2的请求:
APIService mApiService = APIUtils.getSOService();
public void callAPIGetInsertPageInfo() {
mApiService.getInsertPageInfoCall().enqueue(new Callback<GetInsertPageInfoResponse>() {
@Override
public void onResponse(Call<GetInsertPageInfoResponse> call, Response<GetInsertPageInfoResponse> response) {
if (response.isSuccessful()) {
Log.i("Response", response.body().toString());
Log.d("GetResponse", response.raw().toString());
} else {
int statusCode = response.code();
// handle request errors depending on status code
}
}
@Override
public void onFailure(Call<GetInsertPageInfoResponse> call, Throwable t) {
//TODO
}
});
}
当我记录响应时,我看到此错误:Response ID 525295 is not served in this process.
答案 0 :(得分:2)
几天后,我发现了自己。我使用HashMap<>
和List<List<Item>>
来匹配那种JSON API。
我声明了一个POJO,其中包含List<Item>
和id
来识别它们
public class ListWithId {
private List<Item> items;
private int id;
public ListWithId(List<Item> items, int id){
this.items = items;
this.id = id;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
在Activity.class
我使用此Object
将所有数据打入
private List<ListWithId> listTC;
public void callAPIGetInsertPageInfo() {
mApiService.getInsertPageInfoCall(Constants.CORE_PARAM, Constants.GET_RESPONSE_PARAM).enqueue(new Callback<GetInsertPageInfoResponse>() {
@Override
public void onResponse(Call<GetInsertPageInfoResponse> call, Response<GetInsertPageInfoResponse> response) {
if (response.isSuccessful()) {
for (Map.Entry<Integer, List<AsobieItem>> entry : response.body().getGetInsertPageInfo().getmPageInfo().entrySet()) {
listTC.add(new ListWithId(entry.getValue(), entry.getKey()));
}
mAdapter = new MyAdapter(Activity.this, listTC);
refreshAdapter();
// set adapter for list view
mListView.setAdapter(mAdapter);
} else {
int statusCode = response.code();
// handle request errors depending on status code
}
}
@Override
public void onFailure(Call<GetInsertPageInfoResponse> call, Throwable t) {
//TODO FAIL
}
});
}
当我获得Logged获取密钥和值时。它会像这样
Keys: 1
Values: "1":[{ListItem}, {ListItem}]
Keys: 2
Values: "2":[{ListItem2}, {ListItem2}]
答案 1 :(得分:1)
您可以使用
HashMap<Integer,List<MyObject> GetInsertPageInfo
答案 2 :(得分:1)
首先,我认为你应该改变你的基本网址。
例如:如果您base_url
现在是abc.com/help/dosth
- &gt;您应该将base_url
更改为abc.com/
他们将API interface
重新实现为
public interface APIService {
@GET("help/dosth")
Call<GetInsertPageInfoResponse> getInsertPageInfoCall(@Query("Service") String service, @Query("Operation") String operation);
}
然后用params调用API,看看会发生什么。在你的代码中我看到POJO对象仍然不完全正确,但让我们稍后再看