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.

1
  • 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. Commented Jun 27, 2019 at 7:18

1 Answer 1

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()

Sign up to request clarification or add additional context in comments.

6 Comments

Yes now it entered in the subscribe ! But I still need to cast it into a List<Lesson>. I already tried the following which is not working : val list = lessons.filterIsInstance<Lesson>() if (list.size != lessons.size){ Log.e("CAST", "CAST FAILED") }
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.
the getSessions is on the server side and return automatically ParseObject
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 ?
Finally after some talk I understood that a ParseObject is like a JSON so I can read data like this : name = parseObject.getString("name")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.