当前位置: 首页 > 知识库问答 >
问题:

如何解决此错误:应为BEGIN_ARRAY,但BEGIN_OBJECT位于第1行第2列路径$

叶越
2023-03-14

请忽略:看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。看起来你的帖子大部分都是代码;请再补充一些细节。

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
}




共有1个答案

郜俊健
2023-03-14

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

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

解决方案

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

 类似资料: