3

Hi I am trying to parse Json from this api https://restcountries.eu. But when i trie to parse the topLevelDomain i ve got the error: "E/Error: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 42 path $[0].topLevelDomain" How can i fix it ? Thanks in advance

Below is Json structure, My Model , and MainActivity

Json structure

[{
"name": "Colombia",
"topLevelDomain": [".co"],
"alpha2Code": "CO",
"alpha3Code": "COL",
"callingCodes": ["57"],
"capital": "Bogotá",
"altSpellings": ["CO", "Republic of Colombia", "República de Colombia"],
"region": "Americas",
"subregion": "South America",
"population": 48759958,
"latlng": [4.0, -72.0],
"demonym": "Colombian",
"area": 1141748.0,
"gini": 55.9,
"timezones": ["UTC-05:00"],
"borders": ["BRA", "ECU", "PAN", "PER", "VEN"],
"nativeName": "Colombia",
"numericCode": "170",
"currencies": [{
    "code": "COP",
    "name": "Colombian peso",
    "symbol": "$"
}],

Model

public class ModelJsona {
private String flag;
private String name;
private String region;
private String topLevelDomain;


public String getFlag() {
    return flag;
}

public void setFlag(String flag) {
    this.flag = flag;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}



public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}


public String getTopLevelDomain() {
    return topLevelDomain;
}

public void setTopLevelDomain(String region) {
    this.topLevelDomain = region;
}

}

MainActivity

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<ModelJsona> dataArrayList;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();




}


private void initViews(){
    recyclerView=(RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    loadJSON();
}

private void loadJSON(){
    dataArrayList = new ArrayList<>();
    Retrofit retrofit=new Retrofit.Builder().baseUrl("https://restcountries.eu/").addConverterFactory(GsonConverterFactory.create()).build();
    RequestInterface requestInterface=retrofit.create(RequestInterface.class);
    Call<List<ModelJsona>> call= requestInterface.getJSON();
    call.enqueue(new Callback<List<ModelJsona>>() {
        @Override
        public void onResponse(Call<List<ModelJsona>> call, Response<List<ModelJsona>> response) {
            dataArrayList = response.body();
            dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
            recyclerView.setAdapter(dataAdapter);
        }

        @Override
        public void onFailure(Call<List<ModelJsona>> call, Throwable t) {
            Log.e("Error",t.getMessage());
        }
    });
}
1
  • A web browser with a plugin such as chrome.google.com/webstore/detail/postman/… can be used to verify if the server you hit is in the expected format. Happens the link you gave is in xml format Commented Dec 27, 2017 at 20:09

1 Answer 1

5

topLevelDomain is an array, not String. You should map it as so:

public class ModelJsona {
    private String flag;
    private String name;
    private String region;
    private List<String> topLevelDomain;
    ...
}

If you need it to be Serializable use ArrayList instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.