1

So, weird thing happening. I am using the following code to serialize my json to a User class using gson:

User user = gson.fromJson(json, User.class);

Here is my user class:

@Entity(tableName = "user")
public class User implements Serializable {
    @PrimaryKey(autoGenerate = true)
    private long id;
    private String fullName;
    private String emailId;
    private String deviceId;
    private String trialId;
    private String macAddress;
    private String timestamp = new SimpleDateFormat("YYYY-MM-dd HH:mm",
            Locale.getDefault()).format(Calendar.getInstance().getTime());

    public User() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getMacAddress() {
        return macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getTrialId() {
        return trialId;
    }

    public void setTrialId(String trialId) {
        this.trialId = trialId;
    }
}

This has been working just fine all this time until I ran the app on Android 6 and I started getting the following exception:

E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): Failed to handle method call
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): java.lang.RuntimeException: Failed to invoke public org.iprd.hiv_subjective_assessment.models.User() with no args
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:118)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.Gson.fromJson(Gson.java:927)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.Gson.fromJson(Gson.java:892)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.Gson.fromJson(Gson.java:841)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.Gson.fromJson(Gson.java:813)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at org.iprd.wits_subjective_assessment.MainActivity.saveUser(MainActivity.java:132)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at org.iprd.wits_subjective_assessment.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:75)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at org.iprd.wits_subjective_assessment.-$$Lambda$MainActivity$3-yCJcZn0XmWJfjUPHlTfbHTdCg.onMethodCall(lambda)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at android.os.Looper.loop(Looper.java:135)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at android.app.ActivityThread.main(ActivityThread.java:7399)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:305)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:365)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at org.iprd.hiv_subjective_assessment.models.User.<init>(User.java:23)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at java.lang.reflect.Constructor.newInstance(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371):  ... 18 more

I am not sure what in the world is going on now. Does Gson not work with Marshmallow?

spongyboss
  • 8,016
  • 15
  • 48
  • 65
  • 1
    Use small y instead of Y https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for simpledateformat. it has nothing to do with gson – Raghunandan Aug 22 '19 at 08:45
  • 1
    `Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'` – Zun Aug 22 '19 at 08:46
  • Guys thank you so much, I did not even see the second part of the exception. Its working now. Does anyone know why it's working on Android 7+ regardless of the "YYYY"? – spongyboss Aug 22 '19 at 08:50
  • @Raghunandan don't you want to post your comment as an answer so I can mark it? – spongyboss Aug 22 '19 at 11:18
  • @spongyboss no. the question is already closed a duplicate. its fine. – Raghunandan Aug 22 '19 at 11:19

0 Answers0