android - 如何使用 Retrofit 和 Django rest-framework 通过 URL 发送和保存数据
问题描述
我正在开发一个 Android 应用程序,我希望能够管理用户(添加、更新、删除、显示)。我正在使用 Retrofit Client 和 Django-Rest-Framework 。我所有的用户信息都可以通过 127.0.0.1:8000/users/?format=json URL 访问。我已经设法在 ListView 中显示我的所有用户,现在我希望能够在 android 应用程序中添加有关我的用户的信息,以便在我访问相关 URL 时显示它们。我希望我解释得很好。有什么建议吗?
解决方案
From the look of things.. your ID field is unique!
So your problem can be solved by creating another page, we call it edit.php. This page will contain code to edit your database based on the id. Here is a sample code which can help.
<?php
function makeChanges(){
$database = new PDO("mysql:host=localhost;dbname=mydatabase;", "admin", "password");
$database -> exec("INSERT INTO `users`(`nom`, `prenom`, `cne`, `email`, `tel`) VALUES('" . $_GET['nom']. "', '" . $_GET['prenom'] . "', '" . $_GET['cne'] . "', '" . $_GET['email'] . "', '" . $_GET['tel'] . "') WHERE `id`='" . $_GET['id'] . "'");
echo "User with id " . $_GET['id'] . " has been edited successfully";
$database = null; //Destroy the database object
}
makeChanges();
?>
That there basically takes requests via HTTP GET and now finds the user with the id and edits the info to match the one in the request.
So from your android app, you use access http://127.0.0.1/edit.php?id=1&nom=Tom&prenom=him&email=someone@here.com&tel=6541900400&cne=A32ADE3
推荐阅读
- angular - Live server 的概念是如何运作的?
- javascript - 如何正确构建 React 项目?
- r - 将 edit_feature 转换为 sp 对象
- oracle - PLS-00103:在预期以下情况之一时遇到符号“EXECUTE”:
- asp.net - 当前的 .NET SDK 不支持面向 .NET Core 3.0 | 3.1 | 5.0 | 6.0
- android - 如何在 androidleaback 库中自定义行的标题项?
- java - 将数据插入数据库android后如何按顺序更改textview
- ldap - 错误启动容器:API 错误(500):无法连接到网络 hlf-ldap_basic:超出上下文期限
- ios - 运行 react-native run-ios 就成功了。但不启动模拟器
- javafx - 如何从根文件夹为 JavaFx 中的标签设置字体?