The following are detailed explanations of the logical implementation steps of using IDEA backend Java development (such as Spring Boot) and Android Studio front-end development app:
1. Technical selection
- Communication Protocol: Recommended to use RESTful API (HTTP/HTTPS)
- Data format: JSON (lightweight and cross-platform)
- Backend framework:Spring Boot
- Android network library:Retrofit + OkHttp
- Interface testing tool:Postman
2. Implementation steps
1. Backend development (IDEA Spring Boot)
// Example: User API interface
@RestController
@CrossOrigin(origins = "*") // Solve cross-domain problems
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
// Create a user
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = (user);
return new ResponseEntity<>(createdUser, );
}
// Get the user list
@GetMapping
public List<User> getAllUsers() {
return ();
}
// Query the user by ID
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
return (id)
.map(user -> new ResponseEntity<>(user, ))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
Key configurations:
#
=8080
=jdbc:mysql://localhost:3306/mydb
-auto=update
2. Front-end development (Android Studio)
Step 1: Add dependencies
// (Module)
dependencies {
implementation '.retrofit2:retrofit:2.9.0'
implementation '.retrofit2:converter-gson:2.9.0'
implementation '.okhttp3:logging-interceptor:4.9.3'
}
Step 2: Create a Data Model
//
data class User(
@SerializedName("id") val id: Long,
@SerializedName("name") val name: String,
@SerializedName("email") val email: String
)
Step 3: Configure Retrofit
//
object ApiClient {
private const val BASE_URL = "http://10.0.2.2:8080/api/" // Android emulator accesses native address
private val okHttpClient = ()
.addInterceptor(HttpLoggingInterceptor().apply {
level =
})
.build()
private val retrofit = ()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(())
.build()
val userService: UserService = (UserService::)
}
Step 4: Define API Interface
//
interface UserService {
@GET("users")
suspend fun getUsers(): Response<List<User>>
@POST("users")
suspend fun createUser(@Body user: User): Response<User>
@GET("users/{id}")
suspend fun getUserById(@Path("id") id: Long): Response<User>
}
Step 5: Initiate a network request
//
class MainActivity: AppCompatActivity() {
private val userService =
override fun onCreate(savedInstanceState: Bundle?) {
(savedInstanceState)
setContentView(.activity_main)
CoroutineScope().launch {
try {
// Get the user list
val response = ()
if () {
val users = ()
users?.forEach { user ->
("API_RESPONSE", "User: ${}")
}
}
} catch (e: Exception) {
()
}
}
}
}
3. Key points of joint adjustment
-
IP address configuration:
- The emulator accesses the machine:
10.0.2.2
- Real-time debugging requires ensuring that the mobile phone and PC are on the same LAN, and the LAN IP of the PC is used.
- The emulator accesses the machine:
-
Network permissions:
<!-- --> <uses-permission android:name="" />
4. Test process
- Testing backend interfaces using Postman
- Start Android emulator
- View Logcat output