【发布时间】:2017-11-12 05:58:44
【问题描述】:
我正在 android 中构建这个演示项目以供临时使用。我需要在服务器数据库上运行查询并从中返回结果。我被困在从服务器部分检索数据。这是我的文件。
主要活动:
public void callit(View view) {
final String url = "http://xyz.000webhostapp.com/login.php?mail=abcxyz@gmail.com";
new Thread() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONTokener tokener = new JSONTokener(json);
JSONArray finalResult = new JSONArray(tokener);
Toast.makeText(MainActivity.this, "This is my Toast message!",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
} catch (JSONException e) {
e.printStackTrace();
}
} else {
//Closes the connection.
try {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
} catch (IOException e) {
}
}
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
e.printStackTrace();
{
}
}
}.start();
}
这是我的 php 脚本文件:
<?php
define('HOST_NAME','localhost');
define('USER_NAME','abc');
define('PASSWORD','xyz');
define('DATABASE','pqr');
$con = mysqli_connect(HOST_NAME,USER_NAME,PASSWORD,DATABASE);
if(!$con)
{
die('Could not connect: ' . mysqli_erro());
}
else
{
$email = $_GET["mail"];
$sql="SELECT * FROM user WHERE email='$email' ";
$result=mysqli_query($con, $sql);
if(!empty($result))
{
while ($row = mysqli_fetch_array($result))
{
$response["mb"]=$row["mobile"];
}
echo(json_encode($response));
}
mysqli_close($con);
}
?>
但我不知道这些数据将如何存储在 json 文件中?正如您从 php 文件中看到的那样,我只需要从数据库中返回给定电子邮件 ID 的手机号码。并且 php 脚本运行良好,我在浏览器中对其进行了测试。
我只需要找到一种方法来将此值发送到我的 android 应用程序并为该手机号码敬酒。
问我问题的任何部分是否有歧义。抱歉英语不好。
【问题讨论】:
-
寻找在 php 中构建一个 rest api,或者如果它是一个学校项目,如果安全性(或正确的做事)不是太大的问题,那么你不需要将它转换为 json ,只需在您的 php 文件中,创建一个简单的 php 页面,您可以在其中获取电话号码作为查询参数 $email = $_GET["mail"];就像你已经得到的那样,只需从 android 简单地抓取页面,你不需要将它转换为 json
-
但是数据将如何从服务器传输到 android 文件..我是 android 和 php 的新手。
-
就像浏览器一样,可以回显,抓取页面,如果有html标签可以去掉