Android application that implements the User class and demonstrates simple object serialization using GSON. This will include the MainActivity, layout file, and necessary modifications to the User class.
Here's the complete code:
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("name")
private String name;
@SerializedName("age")
private int age;
@SerializedName("email")
private String email;
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
// Getters
public String getName() { return name; }
public int getAge() { return age; }
public String getEmail() { return email; }
// Setters
public void setName(String name) { this.name = name; }
public void setAge(int age) { this.age = age; }
public void setEmail(String email) { this.email = email; }
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
Button serializeButton = findViewById(R.id.serializeButton);
serializeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performSerialization();
}
});
}
private void performSerialization() {
User user = new User("John Doe", 30, "john.doe@example.com");
Gson gson = new Gson();
String json = gson.toJson(user);
String result = "User object: " + user.toString() + "\n\nSerialized JSON: " + json;
resultTextView.setText(result);
}
}
xml
Copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/serializeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Serialize User" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp">
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
build.gradle (app-level) Make sure you have the GSON dependency in your app-level build.gradle file: gradle
Copy
dependencies {
// ... other dependencies
implementation 'com.google.gson:gson:2.8.9'
}
try :→ To run this application:
Create a new Android project in Android Studio. Replace the content of your MainActivity.java with the provided code. Create a new Java class called User and paste the provided User class code. Replace your activity_main.xml content with the provided layout. Add the GSON dependency to your app-level build.gradle file if it's not already there. Sync your project with Gradle files. Run the application on an emulator or physical device. When you run the app, you'll see a "Serialize User" button. Clicking this button will create a User object, serialize it to JSON using GSON, and display both the original User object and the serialized JSON string on the screen.
This implementation completes the objective of Drill 1: Simple Object Serialization, demonstrating how to serialize a User object to JSON using GSON in an Android application.