我该如何解决此错误:预期BEGIN_ARRAY但在第1行第2列路径$

如何解决此问题? 我不知道解决方法。 检查我的代码并帮助我。

请忽略:看来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。

class Informationactivity : AppCompatactivity() {
    private val _tag = Splashactivity::class.java.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_information)


        val uniqueId = SharedPreference.getidInfo(this)

        val token = SharedPreference.getUserInfo(this)


        Client.retrofitService.profile(uniqueId,token)
            .enqueue(object : Callback<LoginResponse> {
                override fun onFailure(call: Call<LoginResponse>,t: Throwable) {

                }

                override fun onResponse(
                    call: Call<LoginResponse>,response: Response<LoginResponse>
                ) {
                    if (response?.isSuccessful == false) { val er = Gson().fromJson(response.errorBody()?.charStream(),ErrorResponse::class.java)
                        Log.d(_tag,"${er.code}:${er.message}")
                        if (er.code == 60203) {
                            Toast.makeText(this@Informationactivity,"",Toast.LENGTH_SHORT).show()
                        }
                    } else if (response?.isSuccessful == true) {
                        Glide.with(applicationContext).asBitmap().load("https://s3.amazonaws.com/appsdeveloperblog/micky.gif").into(imageView)
                        Toast.makeText(this@Informationactivity,Toast.LENGTH_LONG).show()

                        val file=File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"micky.gif")
                        var fileName="micky.gif"

                        val token = SharedPreference.getUserInfo(applicationContext)
                        val uniqueId= SharedPreference.getidInfo(applicationContext)

                        var requestBody: RequestBody = RequestBody.create(MediaType.parse("image/*"),file)
                        val body: MultipartBody.Part = MultipartBody.Part.createFormData("profile",fileName,requestBody)

                            if (uniqueId != null) {
                                Client.retrofitService.updateProfile(token,uniqueId,body)
                                    .enqueue(object : Callback<List<LoginResponse>> {
                                        override fun onFailure(
                                            call: Call<List<LoginResponse>>,t: Throwable) { Log.d("",t.message) }

                                        override fun onResponse(
                                            call: Call<List<LoginResponse>>,response: Response<List<LoginResponse>>) { if (response?.isSuccessful)
                                        { Toast.makeText(this@Informationactivity,"File Uploaded Successfully...",Toast.LENGTH_LONG).show()
                                                Log.d("","" + response?.body().toString())
                                            } else {
                                                Toast.makeText(this@Informationactivity,"Some error occurred...",Toast.LENGTH_LONG).show()
                                            }
                                        } }) }
                        }
                    }
            }) }
}

interface API {
    @Headers("Content-Type: application/json","Authorization:token:String")

    @Multipart
    @PUT("/user/profile/{userId}")
    fun updateProfile(@Header("Authorization") token: String?,@Path("userId") userID: String,@Part file: MultipartBody.Part): Call<List<Loginresponse>>

}
object Client {
    var retrofitService: API

    init {
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val logger: OkHttpClient = OkHttpClient.Builder().addInterceptor(interceptor).readTimeout(20,TimeUnit.SECONDS).writeTimeout(20,TimeUnit.SECONDS).build()

        val retrofit = Retrofit.Builder()
            .baseUrl("myurl")
            .addConverterFactory(GsonConverterFactory.create())
            .client(logger)
            .build()

        retrofitService = retrofit.create(API::class.java)
    }
}
    @SerializedName("uniqueId")
    val user:String?=null

    @SerializedName("nickname")

    val nickname: String?=null

    @SerializedName("birth")

    val birth: String?=null

    @SerializedName("profileImage")

    val profileImage: String?=null

    @SerializedName("profileThumbnail")

    val profileThumbnails: String?=null

    @SerializedName("gender")

    val gender: Int?=null

    @SerializedName("token")
    val token: String? = null
}




lylyly1988 回答:我该如何解决此错误:预期BEGIN_ARRAY但在第1行第2列路径$

您的json返回为JSON对象。但是您正在尝试转换为Json数组

 Call<List<Loginresponse>> - you try to convert result as JSON Array (list) 

解决方案

获取原始json结果并使用http://www.jsonschema2pojo.org/转换pojo,然后重试

,

您正在尝试将json对象存储在列表中,这就是您得到error的原因。 检查您的JSON响应以{大括号开头,这表示它是对象而不是数组。数组以[方括号。

@PUT("/user/profile/{userId}")
fun updateProfile(@Header("Authorization") token: String?,@Path("userId") userID: String,@Part file: MultipartBody.Part): Call<List<Loginresponse>>

在您使用Call<List<Loginresponse>>方法的所有位置用Call<Loginresponse>替换updateProfile

本文链接:https://www.f2er.com/3119834.html

大家都在问