【发布时间】:2017-09-29 07:02:31
【问题描述】:
我正在尝试在 android 应用程序中显示使用 php 从服务器检索到的信息,我已检索到带有 json 格式的字符串中的信息,这里是检索代码和 php 代码。 PHP 代码
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
//Getting values
$username = $_POST['username'];
require_once('dbConnect.php');
$sql = "SELECT * FROM `employee` WHERE username='$username'";
$result=mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo json_encode($row);
}
Android 检索代码
@Override
protected String doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
HashMap<String,String> params1= new HashMap<>();
params1.put(Config.KEY_EMP_UN,mUsername);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URl_RET, params1);
return res;
// TODO: register the new account here.
}
@Override
protected void onPostExecute(final String success) {
Toast.makeText(Main2Activity.this,success,Toast.LENGTH_LONG).show();
final TextView Textview1 = (TextView)findViewById(R.id.textView);
Textview1.setText(success);
}
想要做的是提取姓名、职务、工资、密码并将它们显示在单独的 TextView 中。我该怎么做?
【问题讨论】:
-
谷歌“android json”。此外,您的 PHP 代码中可能存在 SQL 注入问题。
-
问之前为什么不先搜索一下?