我试图从此api中获取一些json:https://api.cointelegraph.com/api/v1/mobile/feed 我想获得每个新闻的“标题”字段。我创建了一个应该获取该字段和该类包装器的类,但是我得到的是一个空列表,而不是空列表,我想这是因为我必须获取一个字段,该字段首先称为“数据”,然后对其进行解析。因此,我将获取“标题”字段的班级更改为获取“数据”字段的班级,但是我仍然得到空列表。我猜这是一个愚蠢的错误,因为它可以与github api一起使用。我将在下面和我的代码中添加某种响应。感谢您的帮助。
Json回复:
{"data":{"news":[{"type":"header","data":{"id":22358,"title":"MIT, Stanford Researchers to Fund New \u2018Globally Scalable\u2019 Cryptocurrency, \u2018Unit-e\u2019","lead":"Major U.S. universities including MIT, Stanford and UC Berkeley have teamed up to fund a new digital currency.","thumb":"https:\/\/s3.cointelegraph.com\/storage\/uploads\/view\/f7a7d42a1cba645f861ba810d2524f77.jpg","published_at":{"date":"2019-01-17 18:52:00.000000","timezone_type":3,"timezone":"Europe\/London"},"share_url":"https:\/\/cointelegraph.com\/news\/mit-stanford-researchers-to-fund-new-globally-scalable-cryptocurrency-unit-e","views":2375,"type":"news","author":"Helen Partz","author_id":545,"tag":"Blockchain","badge":{"title":"News","label":"default"},"isSponsored":false,"audio":"https:\/\/s3.cointelegraph.com\/audio\/22358.71732d08-dc28-4787-98b1-46c4e0317916.mp3"}},{"type":"currencies","data":[{"price":3677.37,"name":"BTC","currency":"USD","isIncreased":1,"difference":"0.89"},{"price":123.57,"name":"ETH","currency":"USD","isIncreased":1,"difference":"0.17"},{"price":31.56,"name":"LTC","currency":"USD","isIncreased":1,"difference":"0.19"},{"price":0.33,"name":"XRP","currency":"USD","isIncreased":1,"difference":"1.22"}]},
接口:
package com.hfad.cointask.service import com.hfad.cointask.model.DataLIst import com.hfad.cointask.model.NewsList import retrofit2.Call import retrofit2.http.* interface CoinClient { @GET("api/v1/mobile/feed") fun getFeed(): Call }
<b>News class:</b>
<pre>
package com.hfad.cointask.model
class News (private var data: String) {
fun getData():String{
return data
}
}
我已经尝试过这样第一次获得冠军
package com.hfad.cointask.model class News (private var title: String) { fun getTitle():String{ return title } }
我的新闻类包装:
package com.hfad.cointask.model class NewsList { var list = mutableListOf() }
我的适配器:
package com.hfad.cointask.adapter import android.support.v7.view.menu.MenuView import android.support.v7.view.menu.MenuView.ItemView import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView.ViewHolder import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import com.hfad.cointask.R import com.hfad.cointask.model.News class CoinAdapter(var values: List): RecyclerView.Adapter() { override fun onBindViewHolder(p0: CoinViewHolder, p1: Int) { val itemTitle: News = values[p1] p0.newsTitle.text = itemTitle.getData() } override fun getItemCount(): Int { return values.size } override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CoinViewHolder { val view: View = LayoutInflater.from(p0.context).inflate(R.layout.news_item_view, p0, false) return CoinViewHolder(view) } class CoinViewHolder(itemView: View): ViewHolder(itemView) { var newsTitle: TextView = itemView.findViewById(R.id.item_title) } }
我的主要活动:
package com.hfad.cointask import android.os.Bundle import android.support.design.widget.BottomNavigationView import android.support.v7.app.AppCompatActivity import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.widget.Toast import com.hfad.cointask.adapter.CoinAdapter import com.hfad.cointask.model.DataLIst import com.hfad.cointask.model.News import com.hfad.cointask.model.NewsList import com.hfad.cointask.service.CoinClient import kotlinx.android.synthetic.main.activity_feed.* import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory class FeedActivity : AppCompatActivity() { var news = mutableListOf() private lateinit var newsRecyclerView: RecyclerView private lateinit var newsAdapter: CoinAdapter private lateinit var layoutManager: LinearLayoutManager private val client = mCoinClient().build() private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.navigation_home -> { message.setText(R.string.title_home) return@OnNavigationItemSelectedListener true } R.id.navigation_dashboard -> { message.setText(R.string.title_dashboard) return@OnNavigationItemSelectedListener true } R.id.navigation_notifications -> { message.setText(R.string.title_notifications) return@OnNavigationItemSelectedListener true } } false } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_feed) newsRecyclerView = findViewById(R.id.news_recycler_view) layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) newsRecyclerView.layoutManager = layoutManager newsAdapter = CoinAdapter(news) newsRecyclerView.adapter = newsAdapter getNews().enqueue(object: Callback{ override fun onResponse(call: Call, response: Response) { var jsonNews = response.body().list news.clear() news.addAll(response.body().list) newsAdapter.notifyDataSetChanged() } override fun onFailure(call: Call?, t: Throwable?) { val toast = Toast.makeText(this@FeedActivity, t.toString(), Toast.LENGTH_SHORT) toast.show() } }) navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener) } private fun getNews() = client.getFeed() class mCoinClient { private val builder = Retrofit .Builder() .baseUrl("https://api.cointelegraph.com/") .addConverterFactory(GsonConverterFactory.create()) private val retrofit: Retrofit by lazy { builder.build() } private val client: CoinClient by lazy { retrofit.create(CoinClient::class.java) } fun build() = client } }
感谢您的帮助
答案 0 :(得分:0)
我认为您应该将界面更改为
interface CoinClient {
@GET("api/v1/mobile/feed")
fun getFeed(): Call<Your Object that you want to receive it with>
}