0

I'm trying to do a Parse request but I got this error :

com.parse.ParseObject cannot be cast to com.mysapp.mys.common.model.Lesson

but it doesn't enter inside the subscribe after the return of the request. The answer of the server is good because I have the log of it on real-time.

I believe I have a problem when I receive the answer and when I cast implicitly with the lambda. But I don't know how to achieve it in another way.

fun getLessons(coach: Boolean, lessonType: LessonType?, date: Date?, position: LatLng, distance: Double): Single<List<Lesson>> {
        val params = hashMapOf<String, Any?>()
        if (lessonType == null){
            params["lessonType"] = ""
        }
        else{

        }
        params["lessonType"] = 12

        params["date"] = date
        params["geoPoint"] = ParseGeoPoint(44.557514, -0.86099)
        params["within"] = 4935.772
        Log.e("params : ", "lessonType: " + params.get("lessonType") + "| date : " + params.get("date").toString()
                + " | geoPoint: " + params.get("geoPoint") + " | within: "+ params.get("within"))
        return ParseObservable.callFunction<List<Lesson>>("getSessions", params).singleOrError()
    }

how I treat it :

disposables.add(repository.getLessons(false, lesson, dateSelected, position, distance.toDouble())
                .observeOn(Schedulers.io())
                .subscribe({ lessons ->
                    Log.e("LESSONS", lessons.toString())
                    removeCanceledLessonsAndEmit(lessons)
                }, Throwable::printStackTrace)
        )

I don't know how Parse is working so that's why I have copied on this

request who already exists in the project :

fun getAverageRating(coachId: String): Single<HashMap<String, Int>> {
        val params = hashMapOf<String, String>()
        params["coachId"] = coachId
        return ParseObservable.callFunction<HashMap<String, Int>>("getAverageRating", params).singleOrError()
    }

how it's treated :

disposables.add(repository.getAverageRating(coachId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ rating ->
                    if (rating > 0) {
                        ratingBar.visibility = View.VISIBLE
                        ratingBar.rating = rating
                    } else
                        ratingBar.visibility = View.GONE

                }, Throwable::printStackTrace)
        )

If you need any details feel free to ask me.

Manuel
  • 14,274
  • 6
  • 57
  • 130
Itoun
  • 3,713
  • 5
  • 27
  • 47
  • In subscribe, I need to convert the `ParseObject` to a **subclass** of `ParseObject`. Do you have any info on that. I get a `ClassCastExpection` when I try to do that. – Deepak Joshi Jun 27 '19 at 07:18

1 Answers1

1

The following method getSessions is returning a ParseObject instead of returning a Lesson type object. Try to check the type and return appropriate model from that.

Change getLessons return type to this

   ParseObservable.callFunction<List<Any>>("getSessions", params).singleOrError()

and treat like this to check return type.

disposables.add(repository.getLessons(false, lesson, dateSelected, position, distance.toDouble())
                .observeOn(Schedulers.io())
                .subscribe({ lessons ->
                    Log.e("LESSONS", lessons.toString())
                    //removeCanceledLessonsAndEmit(lessons)
                }, Throwable::printStackTrace)
        )

To get lessons in com.mysapp.mys.common.model.Lesson POJO you can do any of the followings

  1. Annotate Lessons class with @ParseClassName("Lesson") See this answer for details

  2. Cast parse object to JSON and use gson to convert to Lesson POJO

Finally, don't forget to do the necessary changes to this line ParseObservable.callFunction>("getSessions", params).singleOrError()

minhazur
  • 4,928
  • 3
  • 25
  • 27
  • Yes now it entered in the `subscribe` ! But I still need to cast it into a `List`. I already tried the following which is not working : `val list = lessons.filterIsInstance() if (list.size != lessons.size){ Log.e("CAST", "CAST FAILED") }` – Itoun Jun 11 '19 at 16:55
  • Casting won't help inside subscribe, you need to return lessons from `getSessions` method. You can add that method in your answer so that I can check. – minhazur Jun 11 '19 at 16:58
  • the getSessions is on the server side and return automatically `ParseObject` – Itoun Jun 11 '19 at 17:04
  • But I'm wondering something, why in the example I gave into my post (`averageRating`) it also returns a `ParseObject` but the `cast` to `Int` is working ? – Itoun Jun 11 '19 at 17:07
  • I think that just works for primitives, not for objects. Please, check the answer for the update. – minhazur Jun 11 '19 at 17:58
  • 1
    Finally after some talk I understood that a `ParseObject` is like a `JSON` so I can read data like this : `name = parseObject.getString("name")` – Itoun Jun 11 '19 at 18:05