首页 > 解决方案 > Android快速网络库获取图像问题

问题描述

我正在使用 Android 快速网络库当我尝试从服务器获取图像时,即使我已成功将所有图像上传到服务器,在取回它们时出现错误 6 个图像

  private fun callProfileApi() {
    binding.progressBar.visibility=View.VISIBLE
    AndroidNetworking.post(Constant.BASE_URL + "app.php?api=userprofile")
        .addUrlEncodeFormBodyParameter("userid", usetId)
        .setTag("userprofile")
        .setPriority(Priority.MEDIUM)

        .build()
        .getAsString(object : StringRequestListener {
            override fun onResponse(response: String) {
                AppLogger.e("MoodMatch response","====="+response)
                Log.d("@@", response)
                var gson: Gson = Gson()
                var userProfileModel = gson.fromJson(response, UserProfileModel::class.java)
                if (!(userProfileModel.error ?: true)){
                    if(userProfileModel.data!=null){
                        updateUI(userProfileModel)
                    }else{
                        AppLogger.toast("Something went wrong")
                    }


                }else{
                    AppLogger.toast(userProfileModel.message)
                }
                binding.progressBar.visibility=View.GONE
            }

            override fun onError(error: ANError) {
                // handle error
                AppLogger.toast(error.localizedMessage)
                Log.e("@@MoodMatc honError", "=======" + error.localizedMessage)
                binding.progressBar.visibility=View.GONE
            }
        })
}

我在这一行出现错误,如下所示:

 var userProfileModel = gson.fromJson(response, UserProfileModel::class.java)

在日志里面,我是这样的

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226 path $.data.images[0]

这是我的模型类:

class UserProfileModel {

 @Expose
 @SerializedName("data")
 var data: Data? = null
 @SerializedName("error")
 var error: Boolean? = null
 @SerializedName("message")
 var message: String? = null 
  }

这是数据类:

class Data {

@SerializedName("about")
var about: String? = null
@SerializedName("account_locked")
var accountLocked: String? = null
@SerializedName("address")
var address: String? = null
@SerializedName("city")
var city: String? = null
@SerializedName("company_policy")
var companyPolicy: String? = null
@SerializedName("created_at")
var createdAt: String? = null
@SerializedName("distance_in")
var distanceIn: String? = null
@SerializedName("dob")
var dob: String? = null
@SerializedName("education")
var education: String? = null
@SerializedName("email")
var email: String? = null
@SerializedName("age")
var age: String? = null
@SerializedName("email_otp")
var emailOtp: String? = null
@SerializedName("fb_link")
var fbLink: String? = null
@SerializedName("first_name")
var firstName: String? = null
@SerializedName("first_time_login")
var firstTimeLogin: String? = null
@SerializedName("gender")
var gender: String? = null
@SerializedName("hobby")
var hobby: String? = null
@SerializedName("id")
var id: String? = null
@SerializedName("image")
var image: String? = null
@SerializedName("insta_link")
var instaLink: String? = null
@SerializedName("intersted_in")
var interstedIn: String? = null
@SerializedName("last_name")
var lastName: String? = null
@SerializedName("login_attempt")
var loginAttempt: String? = null
@SerializedName("membership_type")
var membershipType: String? = null
@SerializedName("membershipid")
var membershipid: String? = null
@SerializedName("mood_id")
var moodId: String? = null
@SerializedName("notification_type")
var notificationType: String? = null
@SerializedName("password")
var password: String? = null
@SerializedName("phone_number")
var phoneNumber: String? = null
@SerializedName("referral_code")
var referralCode: String? = null
@SerializedName("role")
var role: String? = null
@SerializedName("social_id")
var socialId: String? = null
@SerializedName("social_type")
var socialType: String? = null
@SerializedName("status")
var status: String? = null
@SerializedName("term_conditions")
var termConditions: String? = null
@SerializedName("updated_at")
var updatedAt: String? = null
@SerializedName("username")
var username: String? = null
@SerializedName("which_age_people")
var whichAgePeople: String? = null
@SerializedName("images")
var mImages: List<Image>? = null

 }

API响应:

{
"error": false,
"data": {
    "id": "149",
    "username": "kamal kumar",
    "email": "kamalbhera96@gmail.com",
    "phone_number": "9057217361",
    "address": "B-209, Rajendra Marg, Bapu Nagar, Jaipur, Rajasthan 302015, India",
    "city": "Jaipur",
    "images": [
        "http://moodsmatch.com/admin/uploads/20191209_151815.jpg",
        "http://moodsmatch.com/admin/uploads/29261010_1348876641925767_1868739767978426368_n.jpg",
        "http://moodsmatch.com/admin/uploads/20191209_112909.jpg",
        "http://moodsmatch.com/admin/uploads/IMG_2ljalw.jpg",
        "http://moodsmatch.com/admin/uploads/20191209_150617.jpg",
        "http://moodsmatch.com/admin/uploads/FB_IMG_1575778091267.jpg"
    ],
    "state": "Rajasthan",
    "country": "India",
    "referral_code": "Fnt4oe7C",
    "referral_points": "0",
    "membership_type": "Free",
    "intersted_in": "",
    "distance_in": "",
    "defaultDistance": "5M",
    "image": "http://www.moodsmatch.com/admin/../uploads/",
    "gender": "Male",
    "age": 0,
    "dob": "12/02/2019",
    "education": "he is a very good person ",
    "hobby": "",
    "about": "he is a very good person "
},
"message": "Record match"
 }

请建议我如何解决这个问题 提前谢谢你,我将不胜感激每一个答案

标签: androidjsonkotlingsonfast-android-networking

解决方案


对图像数组使用以下声明。

@SerializedName("images") var mImages: List<String>? = null 

推荐阅读