我每次都收到此錯誤:預計begin_array,但是begin_object我不知道是否需要添加反序列化器或warrper類來改造,但我不知道我可以做一個簡單的方法來滿足我所有的項目我的API JSON響應,這是我改裝代碼嘗試:Retrofit 2.0.0 beta2:預計begin_array,但是begin_object
-
的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 + ")");
}
}
}
仍然給我同樣的錯誤 – Mourad
我仍然有同樣的錯誤這裏是我的代碼:collabedit.com/qbn3p – Mourad
只是使用列表。我的事情會解決你的問題 –