简体   繁体   English

如何将数据从php发送和检索到Java / Android?

[英]How to send and retrive data from php to java/android?

I am building this demo project in android for temporary use.I need to run query on server database and return result from that.I am stuck at retriving data from server part. 我正在临时使用android构建这个演示项目。我需要在服务器数据库上运行查询并从中返回结果。我被困在从服务器部分检索数据。 This are my files. 这是我的文件。
MainActivity : 主要活动 :

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();
}    

And this is my php script file : 这是我的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);
}
?>     

But i dont know how exactly this data is going to store in json file? 但是我不知道这些数据将如何存储在json文件中? As you can see from php file i just need to return mobile number of given email id from database. 从PHP文件中可以看到,我只需要从数据库中返回给定电子邮件ID的手机号码。 And php script is just working fine i tested it in browser. 而且PHP脚本运行良好,我在浏览器中对其进行了测试。

I just need to find out a way to send this value to my android app and toast that mobile number. 我只需要找出一种方法将此值发送到我的android应用程序并举起该手机号码。

Ask if any part of my question feel ambiguous. 询问我的问题是否有任何歧义。 Sorry for bad english. 对不起,英语不好。

its good if you develop a rest api in php, here is an example online tutorial attached for rest, or if security( or proper doing the things) is not much of a concern if its a school project then simply you dnt need to convert it to a json , just in your php file ,create a simple php page, where you get phone number as a query parameter $email = $_GET["mail"]; 如果您使用php开发rest api,则很好, 是附带的示例在线教程,或者如果它是学校项目,那么如果安全性(或适当做事)没有太大的问题,那么您只需将其转换即可到一个json文件中,仅在您的php文件中,创建一个简单的php页面,您可以在其中获取电话号码作为查询参数$ email = $ _GET [“ mail”]; like this which you are already getting, just grab the page from android simple you dnt need to convert it to json here is the Simple PHP program that echos phone number and java code that grabs that phone number 就像您已经得到的一样,只需从android抓取页面,您就不需要将其转换为json,这是简单的PHP程序,它回显电话号码和获取该电话号码的Java代码

<?php
echo "00923244549369"
?>

in your code you will echo the phone number at the end just like it 在您的代码中,您将像在代码末尾那样回显电话号码

public static void main(String args[]) {

    URL url;
    InputStream is = null;
    BufferedReader br;
    String line;

    try {
        url = new URL("http://localhost/writers/");
        is = url.openStream(); // throws an IOException
        br = new BufferedReader(new InputStreamReader(is));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (Exception mue) {
        mue.printStackTrace();
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }

}

output: 00923244549369 输出: 00923244549369

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

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