首页 > 解决方案 > 如何使用 Retrofit 和 Django rest-framework 通过 URL 发送和保存数据

问题描述

我正在开发一个 Android 应用程序,我希望能够管理用户(添加、更新、删除、显示)。我正在使用 Retrofit Client 和 Django-Rest-Framework 。我所有的用户信息都可以通过 127.0.0.1:8000/users/?format=json URL 访问。我已经设法在 ListView 中显示我的所有用户,现在我希望能够在 android 应用程序中添加有关我的用户的信息,以便在我访问相关 URL 时显示它们。我希望我解释得很好。有什么建议吗?

标签: androidjsondjango-rest-frameworkretrofit2

解决方案


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


推荐阅读