realm is generating � while retrieving data which is encoded in utf-8 - android

I am using realm to store and retrieve utf-8 for nepali(Devnagari) characters but when retrieving � is also generating which is not a nepali(Devnagari) character and not stored. How to solve this issue? Along with realm I have used Gson and retrofit.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final RealmResults<Information> realmObj = realm.where(Information.class).findAll();
if (realmObj != null) {
List<Information> obj = realm.copyFromRealm(realmObj);
String json = gson.toJson(obj);
}
which generates
२५ द���खि ४० वर्षः

Related

how to convert string array JSON to be ArrayList in Kotlin?

so I have this JSON in string format:
[
"1.0",
"1.1",
"1.2"
]
but I need to convert it to ArrayList In Kotlin ? I have tried to find, but most of the answer are in Java. if using gson in Java maybe it will be like this. but I am a beginner and I failed to convert the code below to Kotlin
Gson gson = new GsonBuilder().create();
ArrayList<String> theList = gson.fromJson(stringObject, new TypeToken<ArrayList<String>>(){}.getType())
what should I do to convert that json string to array list in Kotlin ?
Maybe this is the kotlin code you want, converted from your Java code:
val gson = GsonBuilder().create()
val theList = gson.fromJson<ArrayList<String>>(stringObject, object :TypeToken<ArrayList<String>>(){}.type)

Android Gson - deserializing null vs absent values

I'd like to understand the difference with a server returning value verses that value being completely absent in the response.
lets take at what i have so far:
data class MyApiResponse(#SerializedName("name") val name: String,
#SerializedName("address") val address: String,
#SerializedName("max_time") val maxTime: Double? = null //this field might BE COMPLETELY absent in response, what will happen here ?
)
regarding the maxTime value, if the value is COMPLETELY absent from server response, will the app crash or will the value be null ?
the issue is im trying to distinguish between server sending
max_time: null vs it being completely absent ,how does gson handle this ?
In both cases it will be null.
Might look here for more info:
Gson optional and required fields
Please try this solution:
String json = ""; //Your json has a String
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
String name = jsonObject.get("name").toString();
String adress = jsonObject.get("address").toString();
//If null, use a default value
JsonElement max_time = jsonObject.get("max_time");
String text = (max_time instanceof JsonNull) ? "" : max_time.getAsString();
String json = ""; //Your json has a String
Gson gson = new GsonBuilder().serializeNulls().create();
MyApiResponse myApiResponse = gson.fromJson(json, YOURCLASSNAME.class);

How to remove specific collections from a JSON with Gson in Shared Preferences?

I want to remove attributes that I specify collections using Gson.
My code:
SharedPreferences sharedPreferences = parentActivity.getSharedPreferences(Accommodation.UPLOAD_ACCOMMODATION_DETAILS_ALL, MODE_PRIVATE);
// Get saved string data in it.
String userInfoListJsonString = sharedPreferences.getString(Accommodation.UPLOAD_ACCOMMODATION_DETAILS, "");
// Create Gson object and translate the json string to related java object array.
Gson gson = new Gson();
Accommodation userInfoDtoArray = gson.fromJson(userInfoListJsonString, Accommodation.class);
// Loop the UserInfoDTO array and print each UserInfoDTO data in android monitor as debug log.
// Get each user info in dto.
Accommodation userInfoDto = userInfoDtoArray;
I printed Json and it have this:
{"accommodationName1":"Green Flat",
"accommodationName2":"Bangolor",
"accommodationName3":"GHost house",
"imgAccommodation1":"/data/user/0/com.xxx.xxx/cache/cropped6900224487159235524.jpg",
"imgAccommodation2":"/data/user/0/com.xxx.xxx/cache/cropped3411178797945328810.jpg",
"imgAccommodation3":"/data/user/0/com.xxx.xxx/cache/cropped9128365945226539316.jpg"}
I can use this code to get the value:
userInfoDto.accommodationName1;
It show the value :
Green Flat
So how I can remove the specific collection of that value?

how to convert sqlite to json in android

How to Convert this
[{destLocId=10, createdUserId=b9ab2d71-9a69-4ba3-b498-d36446a154d6,
createdDate=2016-6-29 14:35:00}]
to this :
[{"destLocId":10, "createdUserId":b9ab2d71-9a69-4ba3-b498-d36446a154d6,
"createdDate":2016-6-29 14:35:00}]
You can use GSON library for converting ArrayList to JSON. Here an example,
ArrayList<String> bibo = new ArrayList<String>();
bibo.add("obj1");
bibo.add("obj2");
bibo.add("obj3");
String json = new Gson().toJson(bibo);
And this example from Gson User Guide to use it on collection.

Gson. converting to JSON a password with the character =

I'm using Gson in an Android app to convert a complicated object to JSON representation. One of the fields is a string called QuickPin containing an encrypted password and the character "=" is converted to "\003d" by Gson.
The Json string is consumed by a C# WEBAPI application, but returns "an error has occurred" message.
The following JSON returns that error message :
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":"MethodCombo":{"AuthMethod":[1]},"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ\u003d\u003d"},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
Meanwhile this JSON works fine :
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":{"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ==","MethodCombo":{"AuthMethod":[1]}},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
Are there a way to force Gson to maintain the string with the password with the = (and others if is the case) characters?
My Android code is :
Gson gson = new Gson();
user = new User();
user.UserAuthentication = new UserAuthentication();
user.UserAuthentication.QuickPin = "mW2n2uTECEtVqWA2B9MzvQ==";
user.UserAuthentication.MethodCombo = new MethodCombo();
user.UserAuthentication.MethodCombo.AuthMethod = new ArrayList<Integer>();
user.UserAuthentication.MethodCombo.AuthMethod.add(1);
user.Status = 0;
String jsonRepresentation = gson.toJson(user);
object.put("user", jsonRepresentation);
Thanks
Gson escapes HTML metacharacters by default. You can disable this behavior.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Categories

Resources