改造 我被卡住了。它显示给我一个错误“改型预期的begin_array但在第1行第2列$是begin_object”,我正在尝试获取json数据 这是我的代码
杰森 [{“ map_number”:“ 2”,“ map_name”:“ 1”,“ map_description”:“ 1”},{“ map_number”:“ 3”,“ map_name”:“ 2”,“ map_description”:“ 2 “]
MainFragment.java
Call<MapItemCollectionDao> call = HttpManager.getInstance().getService().loadMapList();
call.enqueue(new Callback<MapItemCollectionDao>() {
@Override
public void onResponse(Call<MapItemCollectionDao> call, Response<MapItemCollectionDao> response) {
if(response.isSuccessful()){
MapItemCollectionDao dao = response.body();
Toast.makeText(getActivity(),dao.getData().get(0).getMapName(),Toast.LENGTH_LONG).show();
}else{
try {
Toast.makeText(getActivity(),response.errorBody().string(),Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<MapItemCollectionDao> call, Throwable t) {
Toast.makeText(getActivity(),t.toString(),Toast.LENGTH_LONG).show();
}
});
MapItemDao.java
public class MapItemDao {
@SerializedName("map_number") private int mapNumber;
@SerializedName("map_name") private String mapName;
@SerializedName("map_description") private String mapDescription;
public int getMapNumber() {
return mapNumber;
}
public void setMapNumber(int mapNumber) {
this.mapNumber = mapNumber;
}
public String getMapName() {
return mapName;
}
public void setMapName(String mapName) {
this.mapName = mapName;
}
public String getMapDescription() {
return mapDescription;
}
public void setMapDescription(String mapDescription) {
this.mapDescription = mapDescription;
}
MapItemCollectionDao.java
public class MapItemCollectionDao {
private List<MapItemDao> data;
public List<MapItemDao> getData() {
return data;
}
public void setData(List<MapItemDao> data) {
this.data = data;
}}
ApiService.java
public interface ApiService {
@GET("map_api.php")
Call<MapItemCollectionDao> loadMapList();}
HttpManager.java
public class HttpManager {
private static HttpManager instance;
public static HttpManager getInstance() {
if (instance == null)
instance = new HttpManager();
return instance;
}
private Context mContext;
private ApiService service;
private HttpManager() {
mContext = Contextor.getInstance().getContext();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://lmtznetwork.info/backend-snru/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service2 = retrofit.create(ApiService.class);
}
public ApiService getService(){
return service2;
}}