Làm cách nào để xây dựng API REST? . Trong hướng dẫn này, chúng tôi sẽ cố gắng trả lời những câu hỏi này và sẽ tạo một hệ thống đăng ký người dùng hoàn chỉnh
Nếu bạn chưa quen với việc phát triển Android thì trước tiên bạn phải học cách phân tích cú pháp bằng hướng dẫn này. Phân tích cú pháp JSON trong Android
Hãy bắt đầu với việc tạo cơ sở dữ liệu và tập lệnh API REST
Nội dung chi tiết
1. Tạo cơ sở dữ liệu
Khởi động máy chủ của bạn và tạo một cơ sở dữ liệu mới với tên android_app
Sau khi tạo cơ sở dữ liệu bây giờ chúng ta cần tạo một bảng người dùng. Sao chép mã dưới đây và chạy nó trong phần SQL của máy chủ của bạn
2. Tạo bảng người dùng
-- -- Table structure for table `users` -- CREATE TABLE `users` [ `id` int[11] NOT NULL, `name` varchar[255] NOT NULL, `email` varchar[255] NOT NULL, `password` varchar[255] NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ] ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Indexes for dumped tables -- -- -- Indexes for table `users` -- ALTER TABLE `users` ADD PRIMARY KEY [`id`]; -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `users` -- ALTER TABLE `users` MODIFY `id` int[11] NOT NULL AUTO_INCREMENT;
Sau khi tạo bảng của người dùng, đây là giao diện của nó trong dạng xem Cấu trúc
3. API REST của PHP
Bây giờ chúng ta cần viết một API PHP đơn giản sẽ được sử dụng để trao đổi dữ liệu giữa ứng dụng và máy chủ web. Thực hiện theo các bước đơn giản dưới đây để tạo API
- Tạo một thư mục mới có tên androidapp bên trong htdocs/www
- Tạo một tệp PHP mới trong androidAppFolder và đặt tên là api. php
Để tránh bất kỳ sự phức tạp nào của các tệp, chúng tôi sẽ chỉ sử dụng một api. php cho toàn bộ chức năng API REST. Bạn có thể sao chép mã dưới đây và dán nó vào api của bạn. tập tin php. Nó có chú thích ở mỗi bước để bạn có thể hiểu nó một cách dễ dàng
api. php
#d11617 #B31718 #FF4081 #FFFFFF
Sau khi tạo và Project Android Studio mới, bây giờ chúng ta cần tạo thêm 4 hoạt động
- giật gânhoạt động
- Đăng nhậpHoạt động
- Hoạt động đăng ký
- Trang chủHoạt động
5. AndroidManifest. xml Quyền INTERNET
6. Thư viện của bên thứ ba Loopj & Picasso.
Chúng tôi sẽ sử dụng loopj để truy cập dữ liệu từ URL và Picasso để tải hình ảnh từ URL trong bản dựng. lớp [Mô-đun. ứng dụng]
implementation 'com.loopj.android:android-async-1.4.9' implementation 'com.squareup.picasso:picasso:2.5.2'
mô-đun. Ứng dụng hoàn thành mã
apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.tutorialscache.loginsignup" minSdkVersion 14 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile['proguard-android.txt'], 'proguard-rules.pro' } } } dependencies { implementation fileTree[dir: 'libs', include: ['*.jar']] implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:support-annotations:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.loopj.android:android-async-1.4.9' implementation 'com.squareup.picasso:picasso:2.5.2' }
7. GlobalClass. java
Tạo một lớp mới GlobalClass và mở rộng nó bằng Ứng dụng. Phiên bản lớp này sẽ giúp chúng tôi truy cập URL CƠ SỞ và cũng cung cấp cho chúng tôi một phiên bản ứng dụng
import android.app.Application; public class GlobalClass extends Application { public static final String BASE_URL = "//192.168.18.8:8888/androidApp/api.php"; // public static final String BASE_URL = "/localhost:8888/androidApp/api.php"; private static GlobalClass singleton; @Override public void onCreate[] { super.onCreate[]; singleton = this; } public static GlobalClass getInstance[] { return singleton; } }
To launch GlobalClass at the start of the application add the application name in the name attribute in of AndroidManifest.xml