使用改造制作新闻应用程序,D/json:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $=BEGIN_ARRAY
Making news application using retrofit, D/json: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
我正在尝试从某些 API 中获取一些数据,并且我正在使用 Toast 来确定是否有响应,但我不断收到此错误:- D/json:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $
BEGIN_ARRAY
提前致谢:)
这是我的 Retrofit 界面
import retrofit2.Call
import retrofit2.http.GET
interface CallableInterface {
@GET("/api/v1/dev_tracker?page=1&source=reddit")
fun getNews(): Call<News>
}
以及我在界面中调用的新闻Class
import com.google.gson.annotations.SerializedName
import java.nio.file.Files.size
class News {
val dev_posts: Array<Article> = arrayOf(Article())
}
class Article {
var content: String = ""
var language: String = ""
var title: String = ""
}
这是我的 activity
fun loadNews() {
val progress: ProgressBar = findViewById(R.id.pb)
val retrofit = Retrofit
.Builder()
.baseUrl("https://newworldfans.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
val callable = retrofit.create(CallableInterface::class.java)
val getNews = callable.getNews()
getNews.enqueue(object : Callback<News> {
override fun onResponse(call: Call<News>, response: Response<News>) {
progress.visibility = View.GONE
val news: News? = response.body()
Toast.makeText(this@DrawerActivity,
"${news?.dev_posts?.get(0)?.content} ", Toast.LENGTH_SHORT).show();
Log.d("jsoon", "Success" )
}
override fun onFailure(call: Call<News>, t: Throwable) {
progress.visibility = View.GONE
Toast.makeText(this@DrawerActivity, "Failed", Toast.LENGTH_SHORT).show();
Log.d("json", "${t.localizedMessage} ")
}
})
您的 API returns 文章直接作为数组而不是将其包装在对象中。
您可以使用:
fun getNews(): Call<List<Article>>
我正在尝试从某些 API 中获取一些数据,并且我正在使用 Toast 来确定是否有响应,但我不断收到此错误:- D/json:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $
BEGIN_ARRAY提前致谢:)
这是我的 Retrofit 界面
import retrofit2.Call
import retrofit2.http.GET
interface CallableInterface {
@GET("/api/v1/dev_tracker?page=1&source=reddit")
fun getNews(): Call<News>
}
以及我在界面中调用的新闻Class
import com.google.gson.annotations.SerializedName
import java.nio.file.Files.size
class News {
val dev_posts: Array<Article> = arrayOf(Article())
}
class Article {
var content: String = ""
var language: String = ""
var title: String = ""
}
这是我的 activity
fun loadNews() {
val progress: ProgressBar = findViewById(R.id.pb)
val retrofit = Retrofit
.Builder()
.baseUrl("https://newworldfans.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
val callable = retrofit.create(CallableInterface::class.java)
val getNews = callable.getNews()
getNews.enqueue(object : Callback<News> {
override fun onResponse(call: Call<News>, response: Response<News>) {
progress.visibility = View.GONE
val news: News? = response.body()
Toast.makeText(this@DrawerActivity,
"${news?.dev_posts?.get(0)?.content} ", Toast.LENGTH_SHORT).show();
Log.d("jsoon", "Success" )
}
override fun onFailure(call: Call<News>, t: Throwable) {
progress.visibility = View.GONE
Toast.makeText(this@DrawerActivity, "Failed", Toast.LENGTH_SHORT).show();
Log.d("json", "${t.localizedMessage} ")
}
})
您的 API returns 文章直接作为数组而不是将其包装在对象中。
您可以使用:
fun getNews(): Call<List<Article>>