1

I'm trying to get news from Guardian API. I'm getting null response, everything is below. I'm using Kotlin, Retrofit and RxJava. I know that there are some miscalled variables/objects but I will change them when I will get rid of that problem.

Retrofit interface

@get:GET("search?api-key=test")
    val news:Observable<News>

Retrofit client

val instance : Retrofit
    get() {
        if (myInstance == null) {
            myInstance = Retrofit.Builder()
                    .baseUrl("https://content.guardianapis.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
        }
        return myInstance!!
    }

And function where I'm loading data

private fun loadUrlData() {
        compositeDisposable.add(jsonApi.news
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe{news -> displayData(news)})
    }

JSON example

{  
   response:{  
      status:"ok",
      userTier:"developer",
      total:2063064,
      startIndex:1,
      pageSize:10,
      currentPage:1,
      pages:206307,
      orderBy:"newest",
      results:[  
         {  
            id:"politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
            type:"article",
            sectionId:"politics",
            sectionName:"Politics",
            webPublicationDate:"2018-09-24T18:57:48Z",
            webTitle:"Keir Starmer: Labour does not rule out remaining in EU as option",
            webUrl:"https://www.theguardian.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
            apiUrl:"https://content.guardianapis.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
            isHosted:false,
            pillarId:"pillar/news",
            pillarName:"News"
         }
      ]
   }
}

Model class

data class News( val status: String, val userTier: String, val total: Int, val startIndex: Int, val pageSize: Int, val currentPage: Int, val pages: Int, val orderBy: String, val results: List<Result>)

I suppose that the problem is with the last function or with the interface but I can't find the solution.

5
  • How does your data model (News) in Kotlin look like? Commented Sep 24, 2018 at 23:16
  • data class News( val status: String, val userTier: String, val total: Int, val startIndex: Int, val pageSize: Int, val currentPage: Int, val pages: Int, val orderBy: String, val results: List<Result> ) Commented Sep 24, 2018 at 23:35
  • Yeah, that's the issue. :-) Your JSON has an outer node (response) and if you're trying to return a News you won't get it, because RetroFit can't map the JSON to the News class. Add an outer class called Response that holds a field called response that is of type News, that should fix it. Commented Sep 24, 2018 at 23:41
  • 1
    Thank you very much! I didn't see that response is another node. As you said I created class Response where I have constructor with News object. Everything works fine now. Thanks again :) Commented Sep 25, 2018 at 12:42
  • No problem - I'll add it as an answer, so you can close the question :) Commented Sep 25, 2018 at 13:42

1 Answer 1

1

The issues lies within your data model class.

Your JSON has an outer node (response) and if you're trying to return a News you won't get it, because Retrofit can't map the JSON to the News class. Add an outer class called Response that holds a field called response that is of type News, that should fix it.

Like so:

class Response(val response: News)

Note: I haven't added data in front of the class as you don't necessarily need it. The data keyword just adds some extra things for you automatically, like toString(), equals() and hashCode(), but unless you're actually using them for anything I wouldn't recommend adding the data keyword as it's pretty useless then.

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.