改装请求后错误-java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$
我正在尝试使用改装、MVVM、协同程序和刀柄发出Post请求 在发送post请求后,我得到了这个错误 java.lang.IllegalStateException:应为BEGIN\u对象,但在第1行第1列路径$处为字符串。 (我已经搜索过了,但没有一个有用) AppModule改装请求后错误-java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$,java,php,android,kotlin,retrofit,Java,Php,Android,Kotlin,Retrofit,我正在尝试使用改装、MVVM、协同程序和刀柄发出Post请求 在发送post请求后,我得到了这个错误 java.lang.IllegalStateException:应为BEGIN\u对象,但在第1行第1列路径$处为字符串。 (我已经搜索过了,但没有一个有用) AppModule @Provides @Singleton fun provideRetrofit(gson: Gson) : Retrofit = Retrofit.Builder() .baseUrl(EndPoints.B
@Provides
@Singleton
fun provideRetrofit(gson: Gson) : Retrofit = Retrofit.Builder()
.baseUrl(EndPoints.BASE_URL)
.client(OkHttpClient.Builder().also { client ->
if (BuildConfig.DEBUG){
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
client.addInterceptor(logging)
}
}.build()
)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@Provides
@Singleton
fun provideApiService(retrofit: Retrofit) = retrofit.create(ApiService::class.java)
@POST(EndPoints.REGISTER_WITH_EMAIL)
suspend fun registerUserWithEmail(@Body newUser: NewUser) : Response<JsonResponse>
suspend fun createNewUserEmail(newUser: NewUser) = apiService.registerUserWithEmail(newUser)
data class JsonResponse (val success: String, val message: String)
ApiService
@Provides
@Singleton
fun provideRetrofit(gson: Gson) : Retrofit = Retrofit.Builder()
.baseUrl(EndPoints.BASE_URL)
.client(OkHttpClient.Builder().also { client ->
if (BuildConfig.DEBUG){
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
client.addInterceptor(logging)
}
}.build()
)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
@Provides
@Singleton
fun provideApiService(retrofit: Retrofit) = retrofit.create(ApiService::class.java)
@POST(EndPoints.REGISTER_WITH_EMAIL)
suspend fun registerUserWithEmail(@Body newUser: NewUser) : Response<JsonResponse>
suspend fun createNewUserEmail(newUser: NewUser) = apiService.registerUserWithEmail(newUser)
data class JsonResponse (val success: String, val message: String)
新用户
data class NewUser (
@SerializedName("username") val userName: String?,
@SerializedName("password") val password: String?,
@SerializedName("email") val email: String?,
@SerializedName("birthday") val birthday: String?,
@SerializedName("birth_year") val birthYear: String?,
@SerializedName("fullname") val fullname: String?,
@SerializedName("gender") val gender: String?)
注册REPO
suspend fun saveUserWithEmail(newUser: NewUser) = safeApiCall {
apiDataSource.createNewUserEmail(newUser) }
注册服务模型
//SaveUserDetails
private val _saveDetailsEmail = MutableLiveData<Resource<JsonResponse>>()
fun doRegisterUserEmail(newUser: NewUser) = viewModelScope.launch {
try {
_saveDetailsEmail.value = registerRepo.saveUserWithEmail(newUser)
}
catch (exception: Exception){
}
}
寄存器片段
val userDetails = NewUser(usernameString, passwordPassed, emailPassed, birthdayPassed, birthYearPassed, fullnameString, genderString)
registerViewModel.doRegisterUserEmail(userDetails)
registerViewModel.saveDetailsEmail.observe(viewLifecycleOwner, Observer {
when(it.status){
Resource.Status.SUCCESS -> {
if(it.data?.success == "0"){
//Display error
Toast.makeText(requireContext(), it.data.message + "Registration", Toast.LENGTH_LONG ).show()
}
else if(it.data?.success == "1"){
//go to home activity
Toast.makeText(requireContext(), it.data.message + "Registrationx", Toast.LENGTH_LONG ).show()
val action = DetailsFragmentDirections.actionDetailsFragmentToHomeActivity()
navController.navigate(action)
}
}
Resource.Status.LOADING -> {
//Show loading
}
Resource.Status.ERROR -> {
Toast.makeText(requireContext(), it.toString(), Toast.LENGTH_LONG).show()
}
}
})
}
这是与邮递员测试后的反应。它起作用了
{"success":"1","message":"User details saved"}
更新资源类
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
enum class Status {
SUCCESS,
ERROR,
LOADING
}
companion object {
fun <T> success(data: T): Resource<T> {
return Resource(Status.SUCCESS, data, null)
}
fun <T> error(message: String, data: T? = null): Resource<T> {
return Resource(Status.ERROR, data, message)
}
fun <T> loading(data: T? = null): Resource<T> {
return Resource(Status.LOADING, data, null)
}
}
}我必须解码发送到服务器的JSON,特别是当这里的内容类型是应用程序/JSON
$data = file_get_contents('php://input');
$json_data = json_decode($data , true);
//I added these above
if ($_SERVER['REQUEST_METHOD']=='POST'){
$username = $json_data["username"];
$password = $json_data["password"];
$email = $json_data["email"];
}
你能发布整个日志吗?实际上,这是网络呼叫响应未成功时引发的唯一异常错误。你能解释一下
资源?资源是一个帮助获取网络呼叫状态的类-(成功、加载或错误)。因此,JsonResponse由一个资源包装,用于正确的错误处理和了解状态。检查更新后的问题。@AdityaKurkure在检查okHttp后,我得到了android中JSON传递的所有值的不精确索引。完整性约束冲突:1048列“用户名”在。。。同时,JSON在LogCat中显示得很好,但行内容类型为text/htm——显示了未定义变量的错误