Android REST API调用,后端没有响应新记录
我正在为课程项目开发android应用程序。我的应用程序有后端(Android REST API调用,后端没有响应新记录,android,json,rest,heroku,kotlin,Android,Json,Rest,Heroku,Kotlin,我正在为课程项目开发android应用程序。我的应用程序有后端(restapi),我是在Heroku上开发和部署的。现在我正试图从后端获取数据。我使用了reformation(networkapi)和Moshi(用于解析JSON字符串) 我第一次成功地从后端获得了3条记录的响应(这些记录是我在rubyonrails中编写API时创建的)。然后为了测试该应用程序,我从Postman发送了POST中的2条记录,然后再次运行该应用程序。这次我没有得到回应。然后我对最后2条添加的记录发送DELETE操作
restapi
),我是在Heroku
上开发和部署的。现在我正试图从后端获取数据。我使用了reformation
(networkapi
)和Moshi
(用于解析JSON
字符串)
我第一次成功地从后端获得了3条记录的响应(这些记录是我在rubyonrails
中编写API
时创建的)。然后为了测试该应用程序,我从Postman
发送了POST
中的2条记录,然后再次运行该应用程序。这次我没有得到回应。然后我对最后2条添加的记录发送DELETE
操作,然后再次运行应用程序,这次我得到了对旧记录的响应。然后,我发送另一个DELETE
操作,从Postman
中删除一条旧记录,并发送POST
操作。然后应用程序显示无响应。我删除了新记录,然后应用程序显示旧记录
通常,应用程序
仅显示旧记录。每当我发送新的POST
操作时,我都没有得到响应。我对正在发生的事情感到惊讶。有人能告诉我有什么问题吗?我已清除手机中的所有缓存
,重新启动手机,重新安装应用程序,但问题仍然存在
(我可以通过邮递员和浏览器成功获取所有数据)
EventApiService.kt
//在这里我定义了改装和Moshi对象
private val BASE_URL = "https://eventnotifierjson2.herokuapp.com/"
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
interface EventApiInterface{
@GET("events")
fun getEvents():
Deferred<List<Event>>
}
object EventApi {
val retrofitService : EventApiInterface by lazy {
retrofit.create(EventApiInterface::class.java)
}
}
}
用于显示回收器视图和图像的BindingAdapters
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<Event>?){
val adapter = recyclerView.adapter as EventsRecyclerAdapter
adapter.submitList(data)
}
@BindingAdapter("imageUrl")
fun bindImage(imgView: ImageView, imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
Glide.with(imgView.context)
.load(imgUri)
.apply(RequestOptions()
.placeholder(R.drawable.ic_gerald_g_boy_face_cartoon)
.error(R.drawable.ic_gerald_g_boy_face_cartoon))
.into(imgView)
}
}
)
fragment\u event\u list.xml//我将ViewHolder充气到的视图
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.mobapproject.eventsroom.modelviews.EventListViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.EventListFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:listData="@{viewModel.eventResponses}"
android:id="@+id/eventsRecyclerView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我遇到了问题,我犯了错误。我向POST操作发送了不正确的参数,Moshi无法解析空值请向我们显示代码或错误日志,这样我们就可以很容易地找出错误所在。@Jayanth没有错误,因为应用程序运行时只有空白片段。我已经编辑了这篇文章,并添加了到目前为止我编写的代码。你能检查一下吗?
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<Event>?){
val adapter = recyclerView.adapter as EventsRecyclerAdapter
adapter.submitList(data)
}
@BindingAdapter("imageUrl")
fun bindImage(imgView: ImageView, imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
Glide.with(imgView.context)
.load(imgUri)
.apply(RequestOptions()
.placeholder(R.drawable.ic_gerald_g_boy_face_cartoon)
.error(R.drawable.ic_gerald_g_boy_face_cartoon))
.into(imgView)
}
}
data class Event(val id: Int,
val title: String,
val description: String,
val country: String,
val city: String,
@Json(name = "event_location") val eventLocation : String,
@Json(name = "event_date") val eventDate : String,
@Json(name = "event_time") val eventTime: String,
@Json(name = "event_status") val eventStatus: String,
@Json(name = "user_id") val userId: Int,
@Json(name = "created_at") val createdAt: String,
@Json(name = "updated_at") val updatedAt: String,
val eventimage : EventImage)
data class EventImage(
val url: String
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.mobapproject.eventsroom.modelviews.EventListViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.EventListFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:listData="@{viewModel.eventResponses}"
android:id="@+id/eventsRecyclerView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardCornerRadius="2dp"
app:cardPreventCornerOverlap="false">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:imageUrl="@{eventsData.eventimage.url}"
android:id="@+id/event_image"
android:padding="0dp"
android:layout_margin="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/event_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical"
android:padding="10dp"
android:id="@+id/container1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/event_title"
android:text="@{eventsData.title}"
android:textColor="#000"
android:textSize="19sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/event_location"
android:text="@{eventsData.eventLocation}"
android:textSize="15sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
{
"id": 1,
"title": "Dummy title",
"description": "Dummy Description",
"country": "Italy",
"city": "Rome",
"event_location": "Rome",
"event_date": "2019-11-26",
"event_time": "2000-01-01T21:28:16.000Z",
"event_status": "open",
"user_id": 1,
"created_at": "2019-11-25T01:51:36.075Z",
"updated_at": "2019-11-25T01:51:36.075Z",
"eventimage": {
"url": "http://res.cloudinary.com/zakstorage/....."
}
}