gson

Retrofit multiple response types

為{幸葍}努か 提交于 2021-02-08 09:13:48
问题 How can I use Retrofit2 to parse these two kinds of API responses? Ok response (HTTP 200): { "data": { "foo": "bar" } } Error response (HTTP 200): { "error": { "foo": "bar" } } I've read tons of SO questions and tutorials, but I don't know how to do that, I've tried: GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(new ItemTypeAdapterFactory()); Gson gson = gsonBuilder.create(); final Retrofit retrofit = new Retrofit.Builder() .client(getOkHttpClient())

Java GSON - serialize int as strings to json file

烈酒焚心 提交于 2021-02-08 06:43:28
问题 I have this Java class: class Car { int mileage; int id; } When I tell gson to serialize it, it of course serializes it to: { "mileage": 123, "id": 12345678 } But what if I want to serialize it to: { "mileage": "123", "id": "12345678" } Assuming changing my members from int to String, is not an option, is there a way to tell gson to serialize those int members as strings to the json file? 回答1: There are likely many ways to achieve what you desire. I will share two ways. FIRST - Using Custom

How to receive a Json array (“result”) with retrofit2

只谈情不闲聊 提交于 2021-02-07 20:39:19
问题 I know how to receive arrays when they are this type: [ { "username": "luis", "job": "developer", "age": 23 } ] my problem is when I must receive an array with a specific name like this: {"result":[{"userid":"1","username":"Luis","job":"developer","age":"23"}]} in this case I must receive the array above with the name "result" using retrofit2. Can anyone please help me I'm new at Retrofit. This is what I have tried: MainActivity apiInterface = ApiClient.getApiClient().create(ApiInterface

Android: Failed to invoke private android.net.Uri() with no args

北战南征 提交于 2021-02-07 18:47:24
问题 I am saving my arraylist of custom model into shared preference using Gson Storing code: ArrayList<DownloadProgressDataModel> arrayList = getArrayListFromPref(downloadProgressDataModel); SharedPreferences.Editor prefsEditor = getSharedPreferences("APPLICATION_PREF", MODE_PRIVATE).edit(); Gson gson = new Gson(); String json = gson.toJson(arrayList); prefsEditor.putString("DownloadManagerList", json); prefsEditor.apply(); } Retrieving ArrayList<DownloadProgressDataModel> arrayList; Gson gson =

GSON serialize sub Object

冷暖自知 提交于 2021-02-07 10:50:29
问题 I want to serialize this JSON structure : { "name":"name 1", "number":1, "standing":[ { "subRank":1, "subname":"test" }, { "subRank":2, "subname":"Test2" }] } And I want to use an object association like that : public class ParentClass{ String name; String number; List<SubClass> SubClassList; } public class SubClass{ String subRank; String subname; } I tried with this code : Type type = new TypeToken<List<ParentClass>>() {}.getType(); ArrayList<ParentClass> parentClassList= new ArrayList<>();

Android crash while using GSON Library and ProGuard

可紊 提交于 2021-02-07 09:44:35
问题 I am using the following Proguard configuration with GSON library: # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over

Android crash while using GSON Library and ProGuard

妖精的绣舞 提交于 2021-02-07 09:43:29
问题 I am using the following Proguard configuration with GSON library: # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over

Android crash while using GSON Library and ProGuard

家住魔仙堡 提交于 2021-02-07 09:43:13
问题 I am using the following Proguard configuration with GSON library: # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over

Cant deserialize object - Expected BEGIN_OBJECT but was STRING at line 1 column 1

江枫思渺然 提交于 2021-02-07 08:51:43
问题 First let me say, I have searched before posting, just cant find the answer. I'm having trouble de-serializing a JSON. It is a valid JSON (checked at http://jsonlint.com/) and it was produced with servicestack json serializer. I'm getting java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 Here is my JSON sent by the server: { "artistName": "", "pathInfo": "C:\\Users\\Unknown\\Desktop\\Audio Sketches\\Chill.mp3", "indexPos": 0, "messageType": "song" } How

Cant deserialize object - Expected BEGIN_OBJECT but was STRING at line 1 column 1

五迷三道 提交于 2021-02-07 08:49:27
问题 First let me say, I have searched before posting, just cant find the answer. I'm having trouble de-serializing a JSON. It is a valid JSON (checked at http://jsonlint.com/) and it was produced with servicestack json serializer. I'm getting java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 Here is my JSON sent by the server: { "artistName": "", "pathInfo": "C:\\Users\\Unknown\\Desktop\\Audio Sketches\\Chill.mp3", "indexPos": 0, "messageType": "song" } How