Implementation example of simple data interaction between android app and PHP Web
preface
As the school's science and technology project needs to realize the simple data interaction between Android App end and PHP web end, the current scenario is that the web end uses MySQL database, Apache server and PHP language. The simple understanding of data interaction is that Android can obtain data from the server and submit data at the same time.
Implementation process
Process description
Concrete implementation
Andorid Server
get data
get_ all_ found_ items.PHP
As can be seen from the above PHP code, through require_ The once() function contains dB_ Connect.php file to execute the database configuration file. Define the array $response to receive the data results of the query, assign $response ["success"] by judging different situations, and return to the web page for display
PHP file execution results
JSON
Submit data
create_ found_ items.PHP
Judge whether all get request parameters exist, and take the obtained get request parameters as data insert to MySQL database. Judge the insert execution process, assign $response ["success"] corresponding to the corresponding $response ["message"] and display it on the web page.
results of enforcement
Andorid
get data
Core code querylosts() function
Using the Android network framework okhttp, an open-source project for processing network requests, okhttp is the hottest lightweight framework on the Android side. Request the interface URL address, obtain the JSON data, and use jsonobject to parse the JSON data.
Submit data
Core code addlost() function
Similarly, okhttp and get are used to submit parameters, try catch is used to obtain exceptions, and a certain prompt of submission result is given through the return value.
Code test
Data synchronization
Web side
Andorid end
Data submission
Submit results
epilogue
The above process is basically realized, and the project can basically be handed over. The PHP part of this project is mainly done by yourself, and it is also done while learning. Android is the main responsibility of another student. During this period, I also asked for the help of friends I made during my internship. Thank all those who give and help. I hope it will help you in your study, and I also hope you can support programming tips.