Android - fetch one to many linked data problem

i need to fetch data(say userlist) from reference data link type (say List of userName) (which is linked as one to many relation), The reference data model (userName) has arraylist of data and when ever i make request i am getting only the first value from arrylist (only first item from userName and other items shows is size =0 ).

It would be grateful if some one helps me out on, what is happening in the request. why the (userName) linked type not returning all values except the one value.

Data model structure : {
“name”: “userList”,
“fields”: [
{
“id”: “title”,
},
{
“id”: “startDate”,
“name”: “Start Date”,
},
{
“id”: “usernames”,
“name”: “usernames”,
“type”: “Array”,
“localized”: false,
“required”: false,
“validations”: [],
“disabled”: false,
“omitted”: false,
“items”: {
“type”: “Link”,
“validations”: [
{
“linkContentType”: [
“userName”
]
}
],
“linkType”: “Entry”
}
},

contentful call:
.where(“content_type”, CONTENTFUL_CONTENT_TYPE)
.all()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer() {
@Override public void accept(CDAArray entries) {
final List tournaments = new ArrayList<>();
for (final CDAResource resource : entries.items()) {
final CDAEntry entry = (CDAEntry) resource;
// Data parsing code goes here
}

                    callback.onCallCompleted();
                }
            }); 

Issue: While calling contentful and looping through the userList-> userName, some of the list in the userName array is returning null or size as zero.

I’m not an android expert, but it does look like your call is a little different from our API documentation:

// Create the Contentful client.
final CDAClient client =
    CDAClient
        .builder()
        .setToken("<access_token>")
        .setSpace("<space_id>")
        .setEnvironment("<environment_id>")
        .build();

client
    .observe(CDAEntry.class)
    .withContentType("<content_type_id>")
    .all()
    // Execute result handling on the `main thread`.
    .observeOn(AndroidSchedulers.mainThread())
    // Networking should happen on io thread.
    .subscribeOn(io.reactivex.schedulers.Schedulers.io())
    .subscribe(
        new DisposableSubscriber<CDAArray>() {
          CDAArray result;

          @Override public void onComplete() {
            // Do something with the result.
            new AlertDialog.Builder(context)
                .setTitle("Contentful")
                .setMessage("Result: "
                    + (result == null ? "<null>" : result.toString()))
                .show();
          }

          @Override public void onError(Throwable e) {
            // Handle error case.
            new AlertDialog.Builder(context)
                .setTitle("Contentful")
                .setMessage("An error occurred: " + e.toString())
                .show();
          }

          @Override public void onNext(CDAArray array) {
            // Array received, maybe more to come.
            result = array;
          }
        }
    );

Are you using the latest version of the Java SDK? The latest version is on GitHub. You might also check out our official JavaDoc site

FYI you can paste in mulit-line code blocks like I did above by typing three backticks ( ` ) at the beginning of a line, then pasting in your code, then end it with three backticks on an empty line

I am getting problem only when there is one to many relationship type of data, other than that it works fine. So i am not sure sdk is the problem.

I am using java-sdk:9.1.0

 static private final CDAClient client = CDAClient
            .builder()
            .setToken(CONTENTFUL_ACCESS_TOKEN)
            .setSpace(CONTENTFUL_SPACE_ID)
            .build();
client.observe(CDAEntry.class)
          .where("content_type", CONTENTFUL_CONTENT_TYPE)
                .all()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<CDAArray>() {
                    @Override public void accept(CDAArray entries) {
                        final List<Tournament> tournaments = new ArrayList<>();
                        for (final CDAResource resource : entries.items()) {
                            final CDAEntry entry = (CDAEntry) resource;
                       
                               // Example code to get Field values 
                              String name = entry.getField("field_name");
                                  if(entry.getField("userName" == null) {
                                              here responce goes as null.
                                  }
                        }
                        
                        callback.onCallCompleted();
                    }
                });

IF you can give some example code for Andorid, on how to fetch one to many data, that would help me lot to figure out.

Hello,

my name is Mario, I am the SDKs and Tools developer for Java and Android.

I noticed you are rather liberal with the names of the fields: In the ContentType response in your first post, it reads userNames as the array field, but you are getting the field called userName in the code sample from your last post. Do you see the missing s in the code snippet? That could be the problem.

If that is not the case, and rather a typo in the code snippet, I would like to ask you to provide us just a bit more information, as in an example of the content where it does not work (screenshot from the web app, or a json snippet you fetched).

Thank you for reaching out to us, and I am happy to solve this issue with you together.

Greetings,
Mario

Hey,

Thanks for your help.

userName i told is just an example. please disregard that.
I have attached an response i got from contentful through android

If you check at the image, there are 2 array list shown, in that both list has an field “competitionRIng” , for first list “competition Ring” is empty and for second list “competitionRing” has value (mapItem).

Actully for both List items “competiion Ring” values (mapItems) are set in the contentful, but when calling through android, i am getting empty response for certain “competion Rings”.

From dataModel view, “competition ring” is mapped to another reference data Model(mapItem), i am getting responce through “competitionring” when i map one particulart item (mapItem) to the “competitionRing”.

I believe i explained the issue, if you need more information, please update me,

Thanks in advance…

Hello,

to rule out an error while parsing the response, do you mind opening a ticket in through our support page, mentioning this post?

This way we could share more private information like the SPACE_ID, ACCESS_TOKEN and the CONTENT_TYPE, and I could take a look at the direct response from Contentful.

Thank you,
Mario

Hello,

Thanks for the information. Will submit the info in support page

1 Like

Hello, Did anyone addressed your issue? I need help with the same one-many relation.

  "name": {},
  "meta": {},
  "items": [
    item1:{},
    item1:{},
    item2:{},
    item2:{}
  ]
}
How do I parse this data using CDAEntry?

For us, the issue was that we weren’t including the correct value for the includes parameter which indicates how many levels of content should be included - https://www.contentful.com/developers/docs/concepts/links/

Updating our code to have that parameter like this solved our issues:

client.observe(CDAEntry.class)
  .where("content_type", CONTENTFUL_CONTENT_TYPE_ID_TOURNAMENT)
  .include(10)
  .all()
  .observeOn(AndroidSchedulers.mainThread())
  .subscribeOn(Schedulers.io())
  .subscribe(..)