简体   繁体   English

使用json从PHP获取数据到textview android,因为不推荐使用httpclient

[英]Get data from PHP to textview android with json, since httpclient is deprecated

I want to ask how to pass data from php to textview in android with json .我想问一下如何使用json将数据从php传递到 android 中的textview I already create this php file to retrieve data from my db, but the thing is I don't know how to pass it to android with json .我已经创建了这个php文件来从我的数据库中检索数据,但问题是我不知道如何使用json将它传递给 android 。 I have read many tutorial, but it doesn't seem to fix my problem since they all using httpclient and httppost which is already deprecated.我已经阅读了很多教程,但它似乎并没有解决我的问题,因为它们都使用已经过时的httpclienthttppost Can u please help me?你能帮帮我吗?

Here is my php file这是我的 php 文件

<? php
mysql_connect("localhost","root","");

mysql_select_db("mydatabase");

$query = "SELECT * FROM tbl_user";
$result = mysql_query($query);


while($row = mysql_fetch_assoc($result))
{
array_push($result,
             array('Name'=>$record[0],
             'Address'=>$record[1],
             'address'=>$record[2]
           ));
         }

    echo json_encode(array("result"=>$result));

?> ?>

Well there are many ways on which you can get data from a server through JSON.好吧,您可以通过多种方式通过 JSON 从服务器获取数据。

Still use HTTP requests - Even though these have depreciated by Android, because of their complexity and lack of speed, you can still use this method to retrieve data from a server.仍然使用 HTTP 请求- 尽管这些已经被 Android 贬低,但由于它们的复杂性和速度不足,您仍然可以使用这种方法从服务器检索数据。 I have tried it and I can assure that it still works on some older apps I myself have developed.我已经尝试过了,我可以保证它仍然适用于我自己开发的一些较旧的应用程序。

Use Volley - In late 2013, Android introduced Volley, which is the best for you to parse JSON data onto your app.使用 Volley - 2013 年底,Android 引入了 Volley,它是您将 JSON 数据解析到您的应用程序的最佳选择。 There are several tutorials online which can help you achieve this.有几个在线教程可以帮助您实现这一目标。 Personally this is my best one.就个人而言, 是我最好的。 Using the Volley API is very similar to the the Http request method, but it is simpler to integrate and faster.使用 Volley API 与 Http 请求方法非常相似,但集成更简单,速度更快。

Use REST - Alternatively you can use REST services to fetch data from the server.使用 REST - 或者,您可以使用 REST 服务从服务器获取数据。 If you consider famous apps such as Evernote or Wunderlist, these keep the user credentials even after uninstalling the app and re-installing it.如果您考虑使用 Evernote 或 Wunderlist 等著名应用程序,即使在卸载应用程序并重新安装后,这些应用程序仍会保留用户凭据。 This is because the unique ID of the device is kept in the database.这是因为设备的唯一 ID 保存在数据库中。 A good tutorial on how to implement this can be found here .可以在此处找到有关如何实现这一点的优秀教程。

Other APIs - A quick search over the internet can provide you with other methods to parse JSON responses from online databases.其他 API - 通过 Internet 快速搜索可以为您提供其他方法来解析来自在线数据库的 JSON 响应。

The best way is to use the volley library.最好的方法是使用排球库。 I find it simply awesome to work with and also easy.我发现使用它简直太棒了,而且也很容易。 It takes care of a lot of work for you.它会为您处理很多工作。 Yes there are always a few limitations to this.是的,这总是有一些限制。 You can go through a few links here , here and here for volley tutorial.你可以通过这里这里这里的几个链接获取凌空教程。 I found this tutorial exceptionally helpful.我发现教程非常有帮助。 Google will give you a lot of results.谷歌会给你很多结果。 Please go through this SO post to get a good idea about your requirements.请仔细阅读这篇SO 帖子,以了解您的要求。 Also @codeByMI gave a really nice answer that you can follow. @codeByMI 也给出了一个非常好的答案,您可以遵循。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM