0

How can I solve this problem?

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
}

   

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • show your json response – Ranjithkumar Nov 12 '19 at 05:12
  • 1
    this error clearly states that you are expecting an array on your app where as your api call returns an object response, try converting all references from `` to `Loginresponse` , also sharing your response would help – Navneet Krishna Nov 12 '19 at 05:14
  • [Similar question already answered, please check](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2?rq=1) – Humxa Moghal Nov 12 '19 at 05:22
  • 1
    [You should check here first, already explained](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2?rq=1) – Humxa Moghal Nov 12 '19 at 05:24

2 Answers2

1

Your json return as JSON object. But you are trying to convert into Json array

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

Solution

Get the raw json result & convert the pojo using http://www.jsonschema2pojo.org/ and try again

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
1

You are trying to store json object in list that's why you are getting error . check your JSON response start with { curly bracket it means it is object not an array . array start with [ square bracket .

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

replace Call<List<Loginresponse>> with Call<Loginresponse> all over where you using updateProfile method

Rahul sharma
  • 1,492
  • 12
  • 26