Reputation: 1
Expected BEGIN_ARRAY but was STRING at line 3 column 26 path $[0].data
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());
}
});
}
}
InterFace
public interface Api_Interface {
@GET("json.php")
Call<List<CountryClas>> getcountry(@Query("action") String action, @Query("umID") String umID
, @Query("OauthToken") String OauthToken);
}
ApiClient
public class ApiClient {
private static final String BaseUrl="http://23.227.133.210/consultapro/";
private static Retrofit retrofit=null;
public static Retrofit getClient(){
if (retrofit==null){
retrofit=new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Model Class
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;
}
}
Second Model Class
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 Response
[ { "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", "Country_Name": "United Kingdom", "Country_Code": "44" } ], "message": "Country list found." } ]
Upvotes: 0
Views: 104
Reputation: 1
//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);
}
Upvotes: 0
Reputation: 26
you can convert your response body to Java Objects/List using Gson. See code sample below
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);
Upvotes: 0
Reputation: 410
I try to connect your api without authentication
http://23.227.133.210/consultapro/json.php?action=getCountry
,
then I get the response like this
[{"status":false,"data":"-->getCountry-->3JfalKdsUf15fsfGIwjcXg== -->","message":"User is not authenticated."}]
in data, it return String instead of ARRAY
So I think because you request with an invalid authentication It will make your model is incorrect -> JsonParseException
Sorry for my bad english
Upvotes: 0
Reputation: 3097
That means your Json data model is wrong. Just use some online json to Java model converter to generate model if you dont know what is wrong.
try this converter
This model should actually work if you posted right Json string
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;
}}
Upvotes: 0
Reputation: 4108
'Expected BEGIN_ARRAY but was STRING' means your member variable which you have define their datatype is not matching, it's expecting as String[] might be, and you may have used Sting , please recheck.
Upvotes: 1