使用 json 从 PHP 获取数据到文本视图 android,因为不推荐使用 httpclient
Get data from PHP to textview android with json, since httpclient is deprecated
我想问一下如何用json
将数据从php
传递到android中的textview
。我已经创建了这个 php
文件来从我的数据库中检索数据,但问题是我不知道如何使用 json
将它传递给 android。我已经阅读了很多教程,但它似乎并没有解决我的问题,因为它们都使用 httpclient
和 httppost
,这已经被弃用了。你能帮帮我吗?
这是我的 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));
?>
您可以通过多种方式从服务器获取数据 JSON。
仍然使用 HTTP 请求 - 尽管这些请求已经贬值 Android,但由于它们的复杂性和速度不足,您仍然可以使用这种方法来检索数据从服务器。我已经尝试过了,我可以保证它仍然适用于我自己开发的一些旧应用程序。
使用 Volley - 在 2013 年底,Android 引入了 Volley,这是您将 JSON 数据解析到您的应用程序中的最佳选择。有几个在线教程可以帮助您实现这一目标。就个人而言 this 是我最好的。使用 Volley API 与 Http 请求方式非常相似,但集成更简单,速度更快。
使用 REST - 或者,您可以使用 REST 服务从服务器获取数据。如果您考虑 Evernote 或 Wunderlist 等著名应用程序,即使在卸载并重新安装应用程序后,这些应用程序仍会保留用户凭据。这是因为设备的唯一 ID 保存在数据库中。可以在 here.
上找到关于如何实现它的很好的教程
其他 APIs - 通过互联网快速搜索可以为您提供其他方法来解析来自在线数据库的 JSON 响应。
最好的方法是使用 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 来更好地了解您的要求。 @codeByMI 也给出了一个非常好的答案,您可以遵循。
我想问一下如何用json
将数据从php
传递到android中的textview
。我已经创建了这个 php
文件来从我的数据库中检索数据,但问题是我不知道如何使用 json
将它传递给 android。我已经阅读了很多教程,但它似乎并没有解决我的问题,因为它们都使用 httpclient
和 httppost
,这已经被弃用了。你能帮帮我吗?
这是我的 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));
?>
您可以通过多种方式从服务器获取数据 JSON。
仍然使用 HTTP 请求 - 尽管这些请求已经贬值 Android,但由于它们的复杂性和速度不足,您仍然可以使用这种方法来检索数据从服务器。我已经尝试过了,我可以保证它仍然适用于我自己开发的一些旧应用程序。
使用 Volley - 在 2013 年底,Android 引入了 Volley,这是您将 JSON 数据解析到您的应用程序中的最佳选择。有几个在线教程可以帮助您实现这一目标。就个人而言 this 是我最好的。使用 Volley API 与 Http 请求方式非常相似,但集成更简单,速度更快。
使用 REST - 或者,您可以使用 REST 服务从服务器获取数据。如果您考虑 Evernote 或 Wunderlist 等著名应用程序,即使在卸载并重新安装应用程序后,这些应用程序仍会保留用户凭据。这是因为设备的唯一 ID 保存在数据库中。可以在 here.
上找到关于如何实现它的很好的教程其他 APIs - 通过互联网快速搜索可以为您提供其他方法来解析来自在线数据库的 JSON 响应。
最好的方法是使用 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 来更好地了解您的要求。 @codeByMI 也给出了一个非常好的答案,您可以遵循。