简体   繁体   English

Kotlin-使用RxJava的改造请求给出空响应

[英]Kotlin - Retrofit request using RxJava gives null response

I'm trying to get news from Guardian API. 我正在尝试从Guardian API获取新闻。 I'm getting null response, everything is below. 我得到空响应,所有内容都在下面。 I'm using Kotlin, Retrofit and RxJava. 我正在使用Kotlin,Retrofit和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 JSON范例

{  
   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. 您的JSON有一个外部节点( response ),如果您尝试返回一个News ,则不会得到它,因为Retrofit无法将JSON映射到News类。 Add an outer class called Response that holds a field called response that is of type News , that should fix it. 添加一个名为Response的外部类,该类包含一个名为News的字段response,该字段应该对其进行修复。

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. 注意:由于您不一定需要data ,因此我没有在课程前面添加data 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. data关键字只是自动为您添加了一些额外的东西,例如toString()equals()hashCode() ,但是除非您实际上将它们用于任何用途,否则我不建议您添加data关键字,因为那样就没有用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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