发送从PHP到一个Android / Java的移动应用程序的响应? [英] Sending a response from PHP to an Android/Java mobile app?

查看:102
本文介绍了发送从PHP到一个Android / Java的移动应用程序的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一张code在我的Andr​​oid应用程序,拿起设备的IMEI和发送IMEI作为参数传递给承载在互联网上的PHP脚本。

I currently have a piece of code in my Android application that picks up the devices IMEI and sends that IMEI as a parameter to a PHP script that is hosted on the Internet.

PHP脚本,然后取IMEI参数,并检查文件,看看是否存在串号文件中,如果是这样我希望能够让我的Andr​​oid应用程序知道IMEI存在。所以基本上我只是希望能够返回True到我的应用程序。

The PHP script then takes the IMEI parameter and checks a file to see if the IMEI exists in the file, if it does I want to be able to let my Android application know that the IMEI exists. So essentially I just want to be able to return True to my application.

这是使用PHP可能吗?

Is this possible using PHP?

下面是我的code到目前为止:

Here is my code so far:

Android的/ Java的

Android/Java

//Test HTTP Get for PHP

        public void executeHttpGet() throws Exception {
            BufferedReader in = null;
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI("http://testsite.com/" +
                        "imei_script.php?imei=" + telManager.getDeviceId()
                        ));
                HttpResponse response = client.execute(request);
                in = new BufferedReader
                (new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                String page = sb.toString();
                System.out.println(page);
                } finally {
                if (in != null) {
                    try {
                        in.close();
                        } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


以上发送IMEI作为参数传递给它捡起来,成功并运行对成功的文件检查的PHP脚本,但是我neeed到再能发出一个积极的响应回从PHP脚本,如果IMEI号码相匹配一个文件中的


The above sends the IMEI as a parameter to the PHP script which picks it up successfully and runs a check against the file successfully, however I neeed to then be able to send a positive response back from the PHP script if the IMEI matches one in the file.

下面是PHP:

<?php
    // to return plain text
    header("Content-Type: plain/text"); 
    $imei = $_GET["imei"];

    $file=fopen("imei.txt","r") or exit("Unable to open file!");

    while(!feof($file))
     {
    if ($imei==chop(fgets($file)))
     echo "True";
     }

    fclose($file);

?>


所以不是真正的呼应我希望能够让我的应用程序知道IMEI被发现,这是可能的,如果是我应该使用能实现吗?


So instead of echo True I want to be able to let my application know that the IMEI was found, is this possible and if so what should I be using to achieve it?

推荐答案

这是好东西!实际上,你就要成功了。你的PHP应该不会改变,你的java应该!你只需要检查在你的java code反应的结果。重新定义你的Java方法

this is good stuff! actually, you're nearly there. your php shouldn't change, your java should! you just need to check the result of the response inside your java code. redeclare your java method as

public String executeHttpGet() {

那么,就让这个方法返回的变量页面。

then, let this method return the variable page.

现在,你可以在某处创建一个辅助方法。如果你把它放在同一类executeHttpGet,它看起来是这样的:

now you can create a helper method somewhere. if you put it in the same class as executeHttpGet, it will look like this:

public boolean imeiIsKnown(){
    return executeHttpGet().equals("True");
}

现在,你可以调用这个方法来看看你的IMEI知道在你的PHP后台。

now you can call this method to find out if your imei is known in your php backend.

这篇关于发送从PHP到一个Android / Java的移动应用程序的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆