Unable to create call adapter for X model for X method - android

Guess, I'm already baffled with what I'm doing, so I'm reaching out to the community.
I have the following:
UserModel
data class UserModel(
val id: String,
val name: String
){}
UserService
#Headers("Accept: application/json")
#POST("register")
fun doRegisterUserTest(
#Query("user_id") userName: String,
#Query("password") passWord: String
): Deferred<UserModel>
UserRepository
fun test(username: String, password: String): List<UserModel>{
Network.createNetworkRequest().create(UserService::class.java).doRegisterUserTest(username, password)
val x: UserService by lazy {Network.createNetworkRequest().create(UserService::class.java)}
val y = x.doRegisterUserTest(username, password)
return y
}
UserViewModel
private val _result = MutableLiveData<List<UserModel>>()
val result: LiveData<List<UserModel>> = _result
fun onRegister(username: String, password: String) {
viewModelScope.launch {
// connect to api server
//_registrationStatus.value = RegistrationStatus.LOADING
try {
_result.value = userRepository.test(username, password)
....
}
}
}
Network Client
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
/**
* Main entry point for dto access. Call like `Network.devbytes.getPlaylist()`
*/
object Network {
fun createNetworkRequest(): Retrofit {
// Configure retrofit to parse JSON and use coroutines
val retrofit = Retrofit.Builder()
.baseUrl("http://10.0.2.2:8081/api/")
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
return retrofit
}
}
In my api, the above (in ideal scenario) will yield the following:
{
"user": [
{
"user_sys_id": 0,
"name": "Blah blah",
}
]
}
I want to read the values from _result.value = userRepository.test(username, password), I presume that test function will return a List> but I don't know how to access the members.
How can I check _result for the values of user_sys_id and name?
How come I'm getting the following (if I convert the response straight to a List)
Unable to create call adapter for Xmodel for method UserService.doRegisterUserTest
Am I missing something?
Likewise, is it fine to do a Retrofit Call if you were already using Coroutines?
From this Unable to create call adapter for retrofit2.Response<...>, it seems that the function should be suspended, but if I do that I would get a warning
Redundant suspend modifier
PS. Although, if I follow repository pattern, I'm able to see that results where written on a local database the values retrieved from the API, however, I don't think that is correct to store data that aren't supposed to be stored in the first place, say for eg. in a Registration, if the registration fails you just want to read the error message directly from the response

Related

How to use the OpenAPI generators retrofit stub with android kotlin client? Response 501 "Not Implemented"

I want to create a REST-API between an Android client and a Spring Boot server.
I created an OpenAPI 3.0 specification and used the CLI generator from https://openapi-generator.tech to create client and server stubs.
The server part works as intended when accessing it with other clients.
For the client side I used the generator for Kotlin with Retrofit2 via the parameter --additional-properties=library=jvm-retrofit2.
What I get is:
A ModelApi interface, defining my endpoint
A Model class, containing my model
An infrastructure package, containing ApiClient, ResponseExt, Serializer, CollectionFormats and a few *Adapter classes
The generated model class (shortened):
data class MapModel (
#Json(name = "id")
val id: kotlin.Long? = null,
#Json(name = "description")
val desc: String? = null
)
The API interface:
interface MapModelApi {
#GET("mapModel")
fun mapModelGet(): Call<kotlin.collections.List<MapModel>>
#DELETE("mapModel/{mapModelId}")
fun mapModelMapModelIdDelete(#Path("mapModelId") mapModelId: kotlin.Int): Call<Unit>
#GET("mapModel/{mapModelId}")
fun mapModelMapModelIdGet(#Path("mapModelId") mapModelId: kotlin.Int): Call<MapModel>
#PUT("mapModel/{mapModelId}")
fun mapModelMapModelIdPut(#Path("mapModelId") mapModelId: kotlin.Int, #Body mapModel: MapModel): Call<Unit>
#POST("mapModel")
fun mapModelPost(#Body mapModel: MapModel): Call<Unit>
#PUT("mapModel")
fun mapModelPut(#Body mapModel: MapModel): Call<Unit>
}
To do a GET request on the element 0, i tried this in my Activity:
val apiClient = ApiClient()
val mapObjectService = apiClient.createService(MapModelApi::class.java)
val call = mapObjectService.mapModelMapModelIdGet(0)
call.enqueue(object : Callback<MapModel> {
override fun onFailure(
call: Call<MapModel>,
t: Throwable
) {
Log.v("retrofit", "call failed")
t.printStackTrace()
}
override fun onResponse(
call: Call<MapModel>,
response: Response<MapModel>
) {
if (response.isSuccessful) {
val mapModel = response.body()
println(mapModel?.id)
} else {
val statusCode = response.code()
println("Http Code: $statusCode")
}
}
})
When I execute this I get a response, but it is always a 501 response "Not Implemented".
How can I fix this? What is missing in the code?
The server is the problem. The GET request returned a body with example data. I have overseen, that the request code sent by the server was not 200, but 501.

Receiving ByteArray data from API through Retrofit2 causes error in my Android App

I'm calling an API in my Android app to receive this ByteArray from the server Picture of ByteArray data from server
I'm using Retrofit2 in order to call the API, and in doing so I've had to specify the Response type. When I specify the Response type to be ByteArray,
#GET("api/getImage")
suspend fun getImage(#Query("user_id") user_id: String
): Response<ByteArray> // This is in my API Class
suspend fun getImage(user_id: String): Response<ByteArray> {
return RetrofitInstance.api.getImage(user_id)
}` // This is in my Repository accessed through ViewModels
I encounter this error " java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $".
So then I tried to receive the Retrofit2 Response as a String type
#GET("api/getImage")
suspend fun getImage(#Query("user_id") user_id: String
): Response<String> // This is in my API Class
suspend fun getImage(user_id: String): Response<String> {
return RetrofitInstance.api.getImage(user_id)
}` // This is in my Repository accessed through ViewModels
instead and then convert it to Bytes, but I encounter a "com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 56 path $" instead.
If it helps, here's my Retrofit Object I refer to in api calls:
object RetrofitInstance {
private val client = OkHttpClient.Builder().apply {
addInterceptor(MyInterceptor())
addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
}.build()
private val retrofit by lazy {
val gson = GsonBuilder().setLenient().create()
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
val api : ApplicationAPI by lazy {
retrofit.create(ApplicationAPI::class.java)
}
}
Would really appreciate the help in debugging this error

How can I build correctly the body post in retrofit 2?

I'm trying to post some data with retrofit 2 but I'm gettins some problems... and don't find any example like this...
This is the body that I have to send:
{
"birthday": "12-01-1987",
"name": bob,
"activity": {
"activity_preferences": {
"user_subjects": [4,7,8],
"user_allergies": [1,6,10],
}
}
}
This is my data class:
data class GenericFormDataEntity(
var birthday: String,
var name: String,
#SerializedName("activity")
var food: ActivityEntity?
)
data class ActivityEntity(#SerializedName("activity_preferences")val activityPreferences: ActivityPreferencesEntity)
data class ActivityPreferencesEntity(#SerializedName("user_Subjects")var userSubjects:List<Int>?,#SerializedName("user_allergies")var userAllergies: List<Int>?)
This is the method that I'm trying to build the json:
fun getUserFormEntity(): String{
val paramObject = JSONObject()
paramObject.put("birthday", birthday)
paramObject.put("name", name)
paramObject.put("activity", getActivityEntity())
return paramObject.toString()
}
private fun getActivityEntity(): ActivityEntity{
return ActivityEntity(ActivityPreferencesEntity(selectedSubjectList, selecteAllergiesList))
}
And this is the json that is returning me:
{\"birthday\":\"23-12-2019\",\"name\":Bob,"activity\":\"ActivityEntity(activity_preferences=ActivityPreferencesEntity(user_Subjects=[4,7,8], user_allergies=[1,6,10])"}"
My question is, how can I get the correct json that I have to send as a body:
#Headers("Accept: application/json")
#POST("xxxxxxxx")
suspend fun saveUserData(#Body userFormData: String)
You need to stringify getActivityEntity using Gson.
Gson.toJson(getActivityEntity())
Also, from your API I infer that you are using retrofit why not pass along the entire instance of GenericFormDataEntity as the body for your API.
For enabling this you need to follow by adding GsonConverterFactory.create(gson) to your retrofit.
Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.callFactory(okHttpClient)
.build()

Singleton that will hold an instance of Retrofit

So I have created a login that will take an username and password input from the user, encode it with Base64 in order to create a token in the format: ("Authorization", AUTH) where AUTH = "Basic " + Base64 encoding of user and password. This is sent via Headers.
So, in the end, it looks like this: Authorization: Basic XXXXXX, where XXXXXX is the user token.
And then it will check whether or not that user exists in the database via an API request.
I am using Retrofit and OkHttp3 in the same class as RetrofitClient and this class is responsible for using the API and adding those Headers.
Later, I use the RetrofitClient class on the Login Activity.
What I need to do now, is make this "token" available to all the other activities by creating a Singleton that will store the data of the Retrofit after a successful login. But I do not know how to do this.
I started learning Kotlin and Android 3 weeks ago.
Here is my code:
GET_LOGIN.kt
interface GET_LOGIN {
#GET("login")
fun getAccessToken() : Call<String>
}
RetrofitClient.kt
class RetrofitClient {
fun login(username:String, password:String){
val credentials = username + ":" + password
val AUTH = "Basic " + Base64.encodeToString(credentials.toByteArray(Charsets.UTF_8), Base64.DEFAULT).trim()
retrofit = init(AUTH)
}
// Initializing Retrofit
fun init(AUTH: String) : Retrofit{
// Creating the instance of an Interceptor
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
// Creating the OkHttp Builder
val client = OkHttpClient().newBuilder()
// Creating the custom Interceptor with Headers
val interceptor = Interceptor { chain ->
val request = chain?.request()?.newBuilder()?.addHeader("Authorization", AUTH)?.build()
chain?.proceed(request)
}
client.addInterceptor(interceptor) // Attaching the Interceptor
//client.addInterceptor(logging) // Attaching the Interceptor
// Creating the instance of a Builder
val retrofit = Retrofit.Builder()
.baseUrl("https://srodki.herokuapp.com/") // The API server
.client(client.build()) // Adding Http Client
.addConverterFactory(GsonConverterFactory.create()) // Object Converter
.build()
return retrofit
}
lateinit var retrofit : Retrofit
fun providesGetLogin(): GET_LOGIN = retrofit.create(GET_LOGIN::class.java)
}
LoginActivity.kt
var RetrofitClient : RetrofitClient = RetrofitClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
loginBtn.setOnClickListener {
val user = userTxt.text.toString()
val pass = passTxt.text.toString()
if (validateLogin(user, pass)){
login(user, pass)
}
}
}
fun validateLogin(user: String, pass: String): Boolean {
if (user == null || user.trim().isEmpty()){
Toast.makeText(this, "Missing Username or Password", Toast.LENGTH_SHORT).show()
return false
}
if (pass == null || pass.trim().isEmpty()){
Toast.makeText(this, "Missing Username or Password", Toast.LENGTH_SHORT).show()
return false
}
return true
}
fun login(user: String, pass: String) {
RetrofitClient.login(user, pass)
val apiLogin = RetrofitClient.providesGetLogin().getAccessToken()
apiLogin.enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
if(response.isSuccessful){
if(response.body()?.code == 0){
Toast.makeText(this#LoginActivity, "Login Successful!", Toast.LENGTH_SHORT).show()
val intent = Intent(this#LoginActivity, List_usersActivity::class.java)
startActivity(intent)
} else {
Toast.makeText(this#LoginActivity, "Login Failed.", Toast.LENGTH_SHORT).show()
}
}
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Toast.makeText(this#LoginActivity, "Login Failed.", Toast.LENGTH_SHORT).show()
}
})
}
}
first and foremost, please use camel case on java and kotlin. We have standards in java and kotlin on programming. And i can see that you are trying to do DI, but, thats not how you do it in Android.
Anyways, you could do this a couple of ways without even using a singleton but by saving it on a storage. Options are Shared Preferences, Local Storage and SQLite. But, if you insist on using a singleton. You can do it like this:
object MySingleton { // This is how you declare singletons in kotlin
lateinit var token: String;
}
EDIT
So, from your comment, it looked like you need to store the token. You could start by using sharedpreferences(database would be better) and store the token there. I assume you don't know how to so here is an example:
val sp = SharedPreferences("sp", 0);
sp.edit().putString("token", theTokenVariable); // not sure of this function
sp.edit().apply(); // you could use commit if you dont mind sharedpreferences to lag your screen(if it ever will)
Now how do you get the token from retrofit? The only way i could help you right now is that you could retrieve the response body from the response variable you receive from onResponse of the retrofit call. From there it is your problem mate. I don't know how your response is formatted, how it should be retrieved etc. A recommendation would be to format it as JSON.

Retrieve JSON response from rxjava/retrofit POST request

I've created a POST request using rxjava and retrofit that successfully hits my backend server and logs the user in (I get a 201 response in my console, all good). However, I want to then retrieve the users access token that is returned, but when I try to access the rxjava result, it just gives me the object I passed to it. Nowhere can I find out how to get the json success response. I have also verified there is in fact a response in Postman, so it's something with how I make this call.
Here is my Retrofit portion
#Headers("Content-Type: application/json")
#POST("api/v1/login")
fun loginTask(#Body credentials: UserLogin)
: Observable<UserLogin>
And the correspoinding API function
class ApiFunctions(val apiService: LunchVoteApi) {
fun provideHello(): io.reactivex.Observable<Hello> {
return apiService.helloMessage()
}
fun loginTask(email: String, password: String): io.reactivex.Observable<UserLogin> {
val credentials: UserLogin = UserLogin(email, password)
return apiService.loginTask(credentials)
}
}
The UserLogin model that is deserialized by Gson
data class UserLogin(
#SerializedName("email") val email: String,
#SerializedName("password") val password: String
)
And finally the call in my LoginActivity
val loginTask = ApiProvider.provideLoginTask()
override fun doInBackground(vararg params: Void): Boolean? {
// TODO: attempt authentication against a network service.
try {
// Simulate network access.
// Thread.sleep(2000)
compositeDisposable.add(
loginTask.loginTask(mEmail, mPassword)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe ({
result ->
System.out.println(result.toString())
}, { error ->
System.out.println(error)
})
)
} catch (e: InterruptedException) {
return false
}
The confusion comes when I try to access the result -> portion of the compositeDisposable call. It just prints out the UserLogin object. Am I missing something here? Thanks.
Turns out I was returning my UserLogin type instead of a pojo object with an access token property.
Changing my retrofit call to #Headers("Content-Type: application/json")
#POST("api/v1/login")
fun loginTask(#Body credentials: UserLogin)
: Observable<AccessToken>
And creating a new model
data class AccessToken(
#SerializedName("accessToken") val email: String
)
I am now able to print out the token. Thanks to #john-oreilly

Categories

Resources