我每次都会收到此错误:预计begin_array但是begin_object 我不知道是否需要添加反序列化程序或warrper类来进行改造,但我不会# 39;我知道如何以一种简单的方式来完成我的所有项目的api json响应,这是我的改造代码尝试:
-
我的Json :
{
categoryDetails: [
{
id: "1",
categoryName: "Fashion"
},
{
id: "2",
categoryName: "Pets"
},
{
id: "3",
categoryName: "Sports"
},
{
id: "4",
categoryName: "Autre1"
},
{
id: "5",
categoryName: "Autre2"
}
]
}
我的源代码
import java.util.List;
import java.io.IOException;
import java.util.List;
import android.util.Log;
import retrofit.Call;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.http.GET;
import retrofit.http.Path;
public class GitHubClient {
public static final String API_URL = "http://192.168.1.22";
public static class Category {
public final String categoryName;
public final int id;
public Category(String categoryName, int id) {
this.categoryName = categoryName;
this.id = id;
}
}
/*-----------------------------------------------------------------------------------------------------------*/
public interface QampaAPI {
@GET("/api/category")
Call<List<Category>> categoryDetailss();
}
/*-----------------------------------------------------------------------------------------------------------*/
public static void main(String... args) throws IOException {
Log.w("retrofit","main()");
// Create a very simple REST adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Create an instance of our GitHub API interface.
QampaAPI apiCall = retrofit.create(QampaAPI.class);
// Create a call instance for looking up Retrofit categoryDetailss.
Call<List<Category>> call = apiCall.categoryDetailss();
// Fetch and print a list of the categoryDetailss to the library.
List<Category> Categorys = call.execute().body();
for (Category Category : Categorys) {
Log.w("retrofit",Category.categoryName + " (" + Category.id + ")");
}
}
}
答案 0 :(得分:2)
根据错误状态,您的JSON响应与您创建的类不匹配。 请尝试以下POJO课程
public static class Category {
public final List<CatergoryItem> categoryItem;
public Category(List<CategoryItem> item) {
this.categoryItem = item;
}
}
class CategoryItem{
public String categoryName;
public int id;
//getter..setter
}
答案 1 :(得分:0)
你是对的,你需要一个包装类来处理外部对象 -
Status
0 NO RESPONSE Show explanation Loading time: 2012
Request headers
foo: bar
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/46.0.2490.80 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
更新您的界面以使用此对象 -
static class CategoryDetails {
List<Category> categoryDetails;
// add getter and setter
}
和你的电话 -
public interface QampaAPI {
@GET("/api/category")
Call<CategoryDetails> categoryDetailss();
}
您需要从Call<CategoryDetails> call = apiCall.categoryDetailss();
CategoryDetails details = call.execute().body();
对象内部访问列表以迭代它。