Android 使用改型在Json中获取数据
应为BEGIN_数组,但在第3行第26列路径$[0]处为字符串。数据Android 使用改型在Json中获取数据,android,json,retrofit,retrofit2,Android,Json,Retrofit,Retrofit2,应为BEGIN_数组,但在第3行第26列路径$[0]处为字符串。数据 public class MainActivity extends AppCompatActivity { Api_Interface api_interface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC
public class MainActivity extends AppCompatActivity {
Api_Interface api_interface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
api_interface=ApiClient.getClient().create(Api_Interface.class);
Call<List<CountryClas>> call=api_interface.getcountry("GetCountry","727","cl1oEntQ32PxZsS3VJnC+H+CY5oLfFLRU5j1H4bg+1g=");
call.enqueue(new Callback<List<CountryClas>>() {
@Override
public void onResponse(Call<List<CountryClas>> call, Response<List<CountryClas>> response) {
Log.e("Res",">>>>>>"+response.body());
}
@Override
public void onFailure(Call<List<CountryClas>> call, Throwable t) {
Log.e("Error",">>>>>>"+t.getMessage());
}
});
}
}
模型类
public class CountryClas {
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("message")
@Expose
private String message;
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class Datum {
@SerializedName("Country_Id")
@Expose
private String countryId;
@SerializedName("Country_Name")
@Expose
private String countryName;
@SerializedName("Country_Code")
@Expose
private String countryCode;
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
//Json响应
[
{
“状态”:正确,
“数据”:[
{
“国家Id”:“101”,
“国家名称”:“印度”,
“国家/地区代码”:“91”
},
{
“国家识别号”:“231”,
“国家名称”:“美国”,
“国家/地区代码”:“1”
},
{
“国家识别号”:“230”,
“国家名称”:“联合王国”,
“国家/地区代码”:“44”
}
],
“消息”:“找到国家/地区列表。”
}
]这意味着您的Json数据模型是错误的。如果您不知道哪里出了问题,只需使用一些在线json到Java模型转换器来生成模型 试试这个 如果您发布了正确的Json字符串,那么这个模型实际上应该可以工作
class Country {
@SerializedName("status")
private Boolean status;
@SerializedName("data")
private List<Data> data = null;
@SerializedName("message")
private String message;
public class Data {
@SerializedName("Country_Id")
private String countryId;
@SerializedName("Country_Name")
private String countryName;
@SerializedName("Country_Code")
private String countryCode;
}}
国家级{
@序列化名称(“状态”)
私有布尔状态;
@SerializedName(“数据”)
私有列表数据=null;
@SerializedName(“消息”)
私有字符串消息;
公共类数据{
@序列化名称(“国家/地区Id”)
私有字符串countryId;
@序列化名称(“国家/地区名称”)
私有字符串countryName;
@序列化名称(“国家/地区代码”)
私有字符串国家代码;
}}
“预期的BEGIN\u数组但was字符串”表示您定义的成员变量的数据类型不匹配,预期的字符串可能是字符串[],并且您可能使用了Sting,请重新检查 我尝试在没有身份验证的情况下连接您的api
http://23.227.133.210/consultapro/json.php?action=getCountry
然后我得到这样的回答
[{“状态”:false,“数据”:-->getCountry-->3JfalKdsUf15fsfGIwjcXg==-->”,“消息”:“用户未通过身份验证”。}]
在数据中,它返回字符串而不是数组
因此,我认为,因为您的请求带有一个无效的身份验证,它将使您的模型不正确->JsonParseException
对不起,我的英语不好您可以使用Gson将响应正文转换为Java对象/列表。请参阅下面的代码示例
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String result = sb.toString();
ApiResponse apiResponse = new Gson().fromJson(result, ApiResponse.class);
req.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
BufferedReader reader=null;
StringBuilder sb=新的StringBuilder();
reader=新的BufferedReader(新的InputStreamReader(response.body().ByTestStream());
弦线;
试一试{
而((line=reader.readLine())!=null){
某人附加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
字符串结果=sb.toString();
ApiResponse ApiResponse=new Gson().fromJson(结果,ApiResponse.class);
//接口中有一些问题需要定义,这里是代码
公共接口Api_接口{
@FormUrlEncoded
@POST(“json.php”)
调用getcountry(@Field(“action”)字符串action,@Field(“umID”)字符串umID
,@Field(“OauthToken”)字符串OauthToken);
}
您的模型类中可能存在重复的问题,请重新检查您的数据类型请共享您的json响应字符串请发布您的json字符串[{“status”:true,“data”:[{“Country_Id”:“101”,“Country_Name”:“India”,“Country_code”:“91”},{“Country_Id”:“231”,“Country_Name”:“United States”,“Country_code”:“1”},{“Country_Id”:“230”,“国家名称”:“联合王国”,“国家代码”:“44”},{“国家Id”:“1”,“国家名称”:“阿富汗”,“国家代码”:“93”},{“国家Id”:“2”,“国家名称”:“阿尔巴尼亚”,“国家代码”:“355”}],“消息”:“找到国家列表”}]->身份验证错误
class Country {
@SerializedName("status")
private Boolean status;
@SerializedName("data")
private List<Data> data = null;
@SerializedName("message")
private String message;
public class Data {
@SerializedName("Country_Id")
private String countryId;
@SerializedName("Country_Name")
private String countryName;
@SerializedName("Country_Code")
private String countryCode;
}}
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String result = sb.toString();
ApiResponse apiResponse = new Gson().fromJson(result, ApiResponse.class);
//There are some issue to define in InterFace here is code
public interface Api_Interface {
@FormUrlEncoded
@POST("json.php")
Call<List<CountryClas>> getcountry(@Field("action") String action, @Field("umID") String umID
, @Field("OauthToken") String OauthToken);
}