简体   繁体   中英

Kotlin - Retrofit request using RxJava gives null response

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM