Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
应为BEGIN_对象,但在第1行第1列路径$Android Refrofit处为字符串_Android_Json_Kotlin_Retrofit - Fatal编程技术网

应为BEGIN_对象,但在第1行第1列路径$Android Refrofit处为字符串

应为BEGIN_对象,但在第1行第1列路径$Android Refrofit处为字符串,android,json,kotlin,retrofit,Android,Json,Kotlin,Retrofit,我试图发布用户名、密码,并从Android中的JSON获取结果。但是在我点击登录按钮后,我得到了这个错误 流程:com.tolgahantutar.bexworkfloww,PID:12957 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$ 授权会话模型 package com.tolgahantutar.bexworkfloww.data.models

我试图发布用户名、密码,并从Android中的JSON获取结果。但是在我点击登录按钮后,我得到了这个错误

流程:com.tolgahantutar.bexworkfloww,PID:12957 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$

授权会话模型

package com.tolgahantutar.bexworkfloww.data.models

import com.google.gson.annotations.SerializedName


data class AuthorizeSessionModel(
    @SerializedName("UserID")
    var UserID: Int,
    @SerializedName("DatePasswordChanged")
    var DatePasswordChanged: String,
    @SerializedName("ForceToChangePassword")
    var ForceToChangePassword: String,
    @SerializedName("LoginType")
    var LoginType: String,
    @SerializedName("UserName")
    var UserName: String,
    @SerializedName("IsActive")
    var IsActive: Boolean,
    @SerializedName("UserFirstName")
    var UserFirstName: String,
    @SerializedName("UserLastName")
    var UserLastName: String,
    @SerializedName("ID")
    var ID: Int
)
授权会话响应

package com.tolgahantutar.bexworkfloww.data.network

import com.google.gson.annotations.SerializedName
import com.tolgahantutar.bexworkfloww.data.models.AuthorizeSessionModel

data class AuthorizeSessionResponse (
    @SerializedName("Value")
    val authorizeSessionModel: AuthorizeSessionModel ,
    @SerializedName("Result")
    val Result : Boolean,
    @SerializedName("Description")
    val Description: String,
    @SerializedName("Code")
    val Code: String
)
MyApi

package com.tolgahantutar.bexworkfloww.data.network

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST

interface VisaServicesApi {
@FormUrlEncoded
@POST("index#!/Visa32Services/Session_AuthorizeSession")

suspend fun userLogin(
    @Field("SessionID") SessionID:Int,
    @Field("AuthorityID") AuthorityID: Int,
    @Field("UserName") UserName: String,
    @Field("Password") Password: String,
    @Field("LoginType") LoginType: String
):Response<AuthorizeSessionResponse>

companion object{
operator fun invoke():VisaServicesApi{
    val logging = HttpLoggingInterceptor()
    logging.setLevel(HttpLoggingInterceptor.Level.BODY)
    val okHttpClient = OkHttpClient.Builder().addInterceptor(logging).build()
    val gson = GsonBuilder()
        .setLenient()
        .create()
return Retrofit.Builder()
    .client(okHttpClient)
    .baseUrl("http://bexfatestv2service.saasteknoloji.com/swagger/ui/")
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build()
    .create(VisaServicesApi::class.java)

}
}
}
AuthViewModel

package com.tolgahantutar.bexworkfloww.ui.auth

import androidx.lifecycle.ViewModel
import com.tolgahantutar.bexworkfloww.data.network.repositories.AuthorizeSessionRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class AuthViewModel (
    private val repository: AuthorizeSessionRepository
):ViewModel(){

suspend fun userLogin(
SessionID : Int,
AuthorityID: Int,
UserName: String,
Password : String,
LoginType: String
)= withContext(Dispatchers.IO){repository.userLogin(SessionID, AuthorityID, UserName, Password, LoginType)}
}
后勤活动

package com.tolgahantutar.bexworkfloww.ui.auth

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.tolgahantutar.bexworkfloww.R
import com.tolgahantutar.bexworkfloww.databinding.ActivityLoginBinding
import com.tolgahantutar.bexworkfloww.util.ApiException
import com.tolgahantutar.bexworkfloww.util.NoInternetException
import kotlinx.coroutines.launch
import org.kodein.di.KodeinAware
import org.kodein.di.android.kodein
import org.kodein.di.generic.instance

class LoginActivity : AppCompatActivity(), KodeinAware {

    override val kodein by kodein()
    private val factory : AuthViewModelFactory by instance<AuthViewModelFactory>()
    private lateinit var binding : ActivityLoginBinding
    private lateinit var viewModel:AuthViewModel




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


binding = DataBindingUtil.setContentView(this,R.layout.activity_login)
        viewModel = ViewModelProvider(this,factory).get(AuthViewModel::class.java)

binding.buttonLogin.setOnClickListener{
loginUser()
}
    }
private fun loginUser(){

    val SessionID = 0
    val AuthorityID = 0
    val userName =binding.editTextUsername.text.toString().trim()
    val password = binding.editTextPassword.text.toString().trim()
    val LoginType = "System"

    lifecycleScope.launch {
        try{
val authResponse = viewModel.userLogin(SessionID,AuthorityID,userName,password,LoginType)

            if(authResponse.Result){
                Toast.makeText(applicationContext, "Login Successfull!", Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(applicationContext, "Login Failed!!", Toast.LENGTH_LONG).show()
            }
        }catch (e: ApiException){
            e.printStackTrace()
        }catch (e: NoInternetException){
            e.printStackTrace()
        }
    }


}
}

您可能需要创建一个数据类来包装
AuthorizeSessionResponse

比如:

data class AuthorizeSessionResponseWrapper (var authorizeSessionResponse: AuthorizeSessionResponse)

你能解释一下这个类应该放在哪里吗。很抱歉问这个问题,但我是Android新手Development@TolgahanTutar根据你发布的回复,你不需要这样做this@AdityaKurkure好的,我明白了。但我还是不知道哪里出了错。我被它卡住了你在日志中得到了想要的响应吗。您可以通过筛选日志中的单词http进行检查。No。我没有从URL得到任何响应。你能再次检查你的JSON吗?你确定这是正确的模式吗?当我从swagger运行JSON时,我意识到我的JSON是正确的。但是当我使用邮递员查看回复时,我得到的代码中充满了HTML@TolgahanTutarurl抛出错误而不是成功响应。您应该检查改装代码中的响应代码
{
  "Value": {
    "UserID": 0,
    "DatePasswordChanged": "2020-09-22T07:26:33.654Z",
    "ForceToChangePassword": 0,
    "LoginType": "string",
    "UserName": "string",
    "IsActive": true,
    "UserFirstName": "string",
    "UserLastName": "string",
    "ID": 0
  },
  "Result": true,
  "Description": "string",
  "Code": "string"
}
data class AuthorizeSessionResponseWrapper (var authorizeSessionResponse: AuthorizeSessionResponse)