Android 使用json url中的数据
我想使用来自 但是我不知道如何从数组中获取数据 这里有一个数组“data”,在这个数组中还有一个数组“children”,只有这样我才能看到我的字段,如作者姓名等。。 应用程序内使用改装+gson Mb需要解析这些数据吗? 我只是在学习,mbu有一些来源是在哪里写的 这是我的模型课Android 使用json url中的数据,android,json,kotlin,gson,retrofit,Android,Json,Kotlin,Gson,Retrofit,我想使用来自 但是我不知道如何从数组中获取数据 这里有一个数组“data”,在这个数组中还有一个数组“children”,只有这样我才能看到我的字段,如作者姓名等。。 应用程序内使用改装+gson Mb需要解析这些数据吗? 我只是在学习,mbu有一些来源是在哪里写的 这是我的模型课 data class News( @SerializedName("title") var title: String?, @SerializedName("author")
data class News(
@SerializedName("title")
var title: String?,
@SerializedName("author")
var author: String?,
@SerializedName("url")
var url: String?,
@SerializedName("created")
var created: String?,
@SerializedName("num_comments")
var num_comments: String?)
和我的网络连接器
object NetworkService {
private const val BASE_URL = "http://www.reddit.com/top.json/"
private val loggingInterceptor = run {
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.apply {
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
}
}
private val baseInterceptor: Interceptor = invoke { chain ->
val newUrl = chain
.request()
.url
.newBuilder()
.build()
val request = chain
.request()
.newBuilder()
.url(newUrl)
.build()
return@invoke chain.proceed(request)
}
private val client: OkHttpClient = OkHttpClient
.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(baseInterceptor)
.build()
fun retrofitService(): ApiNews {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
.create(ApiNews::class.java)
}
}无需通过此改装的基本url即可从url获取数据 创建接口
interface ClientService {
@GET
fun getData(@Url url:String): Call<Any>
}
接口客户端服务{
@得到
fun getData(@Url:String):调用
}
而不是用这个
val retrofit = Retrofit.Builder().baseUrl("http://www.reddit.com/").addConverterFactory(GsonConverterFactory.create()).build()
val service = retrofit.create(ClientService::class.java)
service?.getData("http://www.reddit.com/top.json")
?.enqueue(object : Callback<Any> {
override fun onFailure(call: Call<Any>, t: Throwable) {
}
override fun onResponse(call: Call<Any>, response: Response<Any>) {
Log.e(TAG, "" + Gson().toJson(response))
if (response.isSuccessful) {
}
}
})
val reformation=reformation.Builder().baseUrl(“http://www.reddit.com/“”.addConverterFactory(GsonConverterFactory.create()).build()
val service=reformation.create(ClientService::class.java)
服务?.getData(“http://www.reddit.com/top.json")
?.排队(对象:回调){
覆盖失效时的乐趣(调用:调用,t:可丢弃){
}
覆盖fun onResponse(调用:调用,响应:响应){
Log.e(标记“”+Gson().toJson(响应))
if(response.issucessful){
}
}
})
检查一下,请注意,我有新的问题。下一个问题是什么?你能告诉我你只需要将这段代码复制到Function fun getData(){在这里跳过改装类,你得到了你的回答}我不知道County的反应是什么?我的错误,让我现在更改一下,更好地尝试一下谢谢,但我仍然不明白我现在应该在“如果”中写些什么。。。Sry(我的编程技能)