0

I'm a newbie in Kotlin. I'm building an app like Twitter. I want to create custom class extends TwitterApiClient - to use more endpoints. Twitter's tutorial is here Tutorial

Here's my code:

class TwitterApiList(session: TwitterSession) : TwitterApiClient(session) {

    fun getHomeTimeline(): TwitterCustom {
        return getService(TwitterCustom::class.java)
    }
}
// TwitterCustom interface
public interface TwitterCustom {
    @GET("/1.1/statuses/home_timeline.json")
    fun home_timeline(@Query("count") count: Int?, @Query("since_id") since_id: Int?, @Query("max_id") max_id: Int?, cb: Callback<List<Tweet>>)
}
// And how I use it
val apiClient = TwitterApiList(TwitterCore.getInstance().sessionManager.activeSession)
apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>() {
    override fun success(result: Result<List<Tweet>>?) {
        Log.d("result", result.toString())
    }

    override fun failure(exception: TwitterException?) {
        Log.d("failed", exception?.message)
    }
})

When I run app, it’s always crash with message “Service methods cannot return void.” at this line

apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>()

Please help me to solve this problem.

Thank you all.

VitDuck
  • 83
  • 9

1 Answers1

0

Your callback is calling methods that return Unit. I.e. success() has no return value, Log.d returns Unit, so the Unit is implied. Same goes for failure().

Look closer at the Callback class. If you have and IDE with "Intelli-sense" or the like, it will help you. Otherwise, look at the interface/class and determine how the methods are defined, specifically, look at the return type and either change your calls to match or change the Callback<T> methods to be Unit (assuming it is your interface). This link can help you figure out how to write your functions to return specific types.

Let's say you find something like this...

interface Callback<T> {
    fun success(result: Result<T>) : T //notice the : T determines the type of the function
    //... other members
}

Then your code might look like this...

apiClient.getHomeTimeline().home_timeline(null, null, null, object : Callback<List<Tweet>>() {
    override fun success(result: Result<List<Tweet>>?) {
        Log.d("result", result.toString())
        return result.getMemberForT() // I made up the name, would need
                                      // decl. for Result<T> 
    }
    //...
}

But without the declarations for Callback and Result, I would only be guessing.

Les
  • 10,335
  • 4
  • 40
  • 60