提问人:user1867845 提问时间:10/25/2023 最后编辑:halferuser1867845 更新时间:10/27/2023 访问量:44
为什么我的 android 应用程序无法在服务器上读取 JSON?
Why is my android application not able to read JSON on server?
问:
我距离将应用程序发布到 Android PlayStore 只有一步之遥。但是,我遇到了一个问题,那就是我的 Android 应用程序(用 Kotlin 编写)无法读取我的服务器 (dreamhost.com) 上的 Json 文件。JSON 的 URL 位于:https://apis.androidparadise.site/contacts/Contacts.json。
data class ContactResponseItem(
val comment: String?,
@SerializedName("data")
val dataList: List<Data?>,
val database: String?,
val name: String?,
val type: String?,
val version: String?
)
我的界面是:
interface ContactApi {
@GET("/contacts/Contacts.json")
suspend fun getAllContacts() : Response<List<ContactResponseItem>>
@POST("/contacts/new")
suspend fun addContact(@Body contact:ContactResponseItem):
Response<ContactResponseItem>
}
我的存储库是:
interface ContactsRepository {
suspend fun getAllContacts(): Resource<List<ContactResponseItem>>
suspend fun addContact(contact: ContactResponseItem):
Resource<ContactResponseItem>
}
我的默认存储库是:
class ContactsRepositoryImpl @Inject constructor(private val api: ContactApi):
ContactsRepository {
override suspend fun getAllContacts(): Resource<List<ContactResponseItem>> {
return try {
val response = api.getAllContacts()
val result = response.body()
if (response.isSuccessful && result != null) {
Resource.Success(result)
}
else {
Resource.Error("Response was not successful")
}
}catch (e: Exception){
Resource.Error (e.message ?: "Failed to retrieve contacts")
}
}
override suspend fun addContact(contact: ContactResponseItem): Resource<ContactResponseItem> {
return try {
val response = api.addContact(contact)
val result = response.body()
Log.d("ContactsRepositoryImpl", response.isSuccessful.toString())
Log.d("ContactRepositoryImpl", (result != null).toString())
if (response.isSuccessful && result != null) {
Resource.Success(result)
}
else {
Resource.Error("Response was not successful")
}
}catch (e: Exception){
Resource.Error (e.message ?: "Failed to retrieve contacts")
}
}
我的 ViewModel 方法之一:
fun getContactEmails(): MutableList<String> {
viewModelScope.launch(dispatchers!!.io) {
when (val loadResponse = contactsRepo.getAllContacts()) {
is Resource.Success ->{
for(i in 0..loadResponse.data?.size!!){
if (loadResponse.data[i].name == "data") {
val dataLoad = loadResponse.data[i].dataList[0]
dataLoad?.let { emailList.add(it.email) }
}
}
_recipientload.value = RecipientLoadingEvent.Success("The first email found: ${emailList[0]}")
}
is Resource.Error<*> -> RecipientLoadingEvent.Failure("Couldn't load client's emails")
else -> _recipientload.value =
RecipientLoadingEvent.Failure(loadResponse.errorMsg.toString())
}
}
return emailList
}
最后,主要活动中的一种方法:
contactsVM.recipientLoad.collect { event ->
when (event) {
is ContactsViewModel.RecipientLoadingEvent.Success -> {
Toast.makeText(this@CreateNewUserRegistration, contactsVM.getContactEmails()[0], Toast.LENGTH_SHORT).show()
}
is ContactsViewModel.RecipientLoadingEvent.Failure -> {
Toast.makeText(
this@CreateNewUserRegistration,
"Failed to retrieve email",
Toast.LENGTH_LONG
).show()
Toast.makeText(
this@CreateNewUserRegistration,
"Failed to send data to server",
Toast.LENGTH_SHORT
).show()
}
else -> {}
}
}
出于某种原因,在主活动中,when 语句中的事件变量返回 ContactsViewModel.RecipientLoadingEvent.Error 而不是 ContactsViewModel.RecipientLoadingEvent.Success。我只收到两个无法检索数据的 Toast。我没有收到来自视图模型的任何错误消息。这只会让我相信 JSON 文件的读取/上传没有正确完成。
答: 暂无答案
评论
catch