Hello everyone, I'm having strange problems with r...
# android
c
Hello everyone, I'm having strange problems with retrofit with moshi and hilt. It either gives the error expecteed Begin_array but found begin_Object, even though I had an array of json objects in the link, or other problems such as malformed json, all fields null response. I'm sharing only the dependency module here (moshi and retrofit instances) and rest will be placed in thread.
Copy code
@Module
@InstallIn(SingletonComponent::class)
object DependencyModule {

    @Singleton
    @Provides
    fun provideMoshi(): Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()

    @Singleton
    @Provides
    fun provideRetrofit(moshi: Moshi): BackendApi = Retrofit.Builder()
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .baseUrl(Constants.BASE_URL)
        .build()
        .create(BackendApi::class.java)
}
Copy code
interface BackendApi {
    @GET("/")
    suspend fun defaultProductFetch(): Notebook

    @GET("photos")
    suspend fun getPhotos(): List<MarsPhoto>

}
Tried the codelab example url, that neither worked.
Copy code
@HiltViewModel
class ProductsViewModel @Inject constructor(private val api: BackendApi): ViewModel(){
val notebookResults: MutableState<Notebook?> = mutableStateOf(null)
val mars: MutableState<List<MarsPhoto>> = mutableStateOf(listOf())

suspend fun getNotebooks(){
    withContext(<http://Dispatchers.IO|Dispatchers.IO>){
        notebookResults.value = api.defaultProductFetch()
    }
}
suspend fun getMarss(){
    withContext(<http://Dispatchers.IO|Dispatchers.IO>){
        mars.value = api.getPhotos()
    }
}
json link: https://jsonkeeper.com/b/6J8T/
Copy code
@Composable
    val jsonProducts: MutableState<Notebook?> = productsViewModel.notebookResults
Error Message 2: after calling asLenient() on moshi
m
@Can Korkmaz Is this response for notebook https://jsonkeeper.com/b/6J8T/ ??
c
@Manish Jain Yes. I also tried with this json array but it gave the error "expected begin_array, found begin_object", with defaultProductFetch return type List<notebook> and everything else the same.
m
@Can Korkmaz Actually for this response you have to change return type of List<Notebook> to NoteBook may be issue in your getPhotos() method please verify your photos response.
c
@Manish Jain GetMars is the example from the google codelab, which has another link with json array. Sorry for the confusion. It neither worked for me. The two links I shared above are for getNotebook(), one is a single Notebook object and one is an array of Notebook objects, I specifiy return types as Notebook and List<Notebook>. When I change link to jsonbin (url for array of objects), it gives the error _Expected BEGIN_ARRAY but was BEGIN_OBJECT at path_, and when I use the other link at jsonkeeper (single json object) it gives the error _Expected BEGIN_OBJECT but found String at path._
c
Can you post your
Notebook
class definition!?
c
Copy code
data class Notebook(
    val sku: String,
    val brand: String,
    val model: String,
    val price: Int,
    val screensize: String,
    val resolution: String,
    val os: String,
    val cpufamily: String?,
    val cpu: String,
    val memory: Int,
    val gpu: String,
    val hdd: Any?,
    val ssd: Int?,
    val quantity: Int,
    val warranty: Any?,
    val warrantytype: Any?,
    val distributor: String,
    val adddate: String?
)
Btw, It fetches MarsPhotos now without a problem, returned the correct number of objects at link. Still can't understand why it doesn't work with my json array, it worked fine with an online rest api.
c
Not sure if this is a problem but
Any
is not a valid json type. I guess there is no adapter for
Any
. Aren't you also missing the annotation?
c
I used the popular json2kotlin plugin, class member names have 1-1 correspondence with json keys. I changed the Any? types to String? and I'll try to test it (if android studio lets me). I'm not making a put or send request, so didn't put a serializable annotation yet but don't know that may be the problem. Update: https://api.jsonbin.io/b/625d3c4bbc312b30ebe8be9d/ still gives the error expected begin_array but found begin_object at path with this link. Reqbin.com specifies content/type as application/json while it specifies it as text/json for the json array url in codelab, maybe it has to do with that. very confused. I will actually need this to work with django rest apis, going to see if the problem is online json keepers. Thank you very much @Chrimaeon @Manish Jain
m
@Can Korkmaz This url https://api.jsonbin.io/b/625d3c4bbc312b30ebe8be9d/ return list of notebook. Can you please show where and how you are calling this url using retrofit. if you don't mind share me your code base in direct message. 😊 I would happy to help you.
195 Views