Code Bug Fix: Display RealmObject using Retrofit | RealmObject&data class difference

Original Source Link

I noticed difference in RealmObject class and normal data class when downloading/displaying JSON with Retrofit. Im not storing it in Realm in this example. Just displaying the data after fetching.
When i use

data class Post (
    var userId: Int? = null,
    var id: Int? = null,
    var title: String? = null,
    var body: String? = null
)

i get normal JSON structure response in my Toast/Log, but when i change model to

open class Post (
    var userId: Int? = null,
    var id: Int? = null,
    var title: String? = null,
    var body: String? = null
): RealmObject

the response is “[email protected]” – like

Below my Api client/interface and Activity

Api client:

interface FormService {

    @GET("posts/1")
    fun getPostById(): Call<Post>

    companion object{

        val okHttpClient = OkHttpClient.Builder()
            .connectTimeout(1, TimeUnit.MINUTES)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build()

        operator fun invoke() : FormService{
            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("https://jsonplaceholder.typicode.com")
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(FormService::class.java)
        }
    }

Activity:

    var call = FormService.invoke().getPostById()
        var data: Post? = null

call.enqueue(object: retrofit2.Callback<Post> {
            override fun onFailure(call: Call<Post>, t: Throwable) {
                Log.v("retrofit", "call failed")
            }

                override fun onResponse(call: Call<Post>, response: retrofit2.Response<Post>) {
                    data = response.body()!!
                    Toast.makeText([email protected], data.toString(), LENGTH_LONG).show()
                    Log.v("retrofit", data.toString())
                }
        })
        }

How do i display it with Realm model? What makes these 2 work diffenet?
Thanks in advance 🙂

Tagged : / / / /

Code Bug Fix: Android retrofit problem sending images in HTTP request

Original Source Link

I’m trying to send images from camera as base64 string to my spring boot backend. I’m using retrofit. The images are located in an object. While debugging the images are loaded in and they are not null. Extra info size for each image is +-2 MB. When I’m trying to send them to my backend in my request they suddently become null. I don’t know what I am doing wrong and I find it very weird that they were not null in my object that I send over HTTP. Any suggestions?

Service:

interface ImageApiService {

    @POST("imageObject")
    fun postNewAccidentStatement(@Body imageObject: ImageObject) : Deferred<ImageObject>
}

ImageObject class:

@Entity(tableName = "imageObject_table")
@SuppressWarnings(RoomWarnings.PRIMARY_KEY_FROM_EMBEDDED_IS_DROPPED)
@TypeConverters(DateConverter::class)
data class ImageObject(

 @TypeConverters(ImageConverter::class)
    @Json(name = "images")
    @Expose
    var imagesAccident: List<Image>? = null
)

Image class:

@Entity(tableName = "image_table")
data class Image (
    @PrimaryKey(autoGenerate = true)
    var id: Int? = 0,
    var base64Img: String? = ""
)

 JSONObject obj= new JSONObject();
 obj.put("file", "data:image/png;base64,"+imgbase64); 
 //add other object field

 //here call api retrofit and send obj.toString()

One possible ways to do it is to use hash map. So you have a base64 image which you had encoded.

HashMap<String, String> map = new HashMap<>();
        map.put("image", image);

And in your retrofit service do this:

@FormUrlEncoded
@POST("image")
Call<ImageClass> getImage(@FieldMap HashMap<String, String> image);

Tagged : / / /