如何从PHP获取变量到Android? [英] How to get a variable from php to android?

查看:115
本文介绍了如何从PHP获取变量到Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在到处寻找这个问题,我见过的唯一的答案是JSON!我觉得还有其他方法可以做到这一点。
我的问题是,我可以从Android的发布数据的PHP脚本将数据插入到我的服务器。但我想要做的是从我的PHP的一些数据到Android。 (不使用JSON)。
请,我仍然在基础知识。使这一尽可能简单!

I have been searching everywhere this question and the only answer i've seen is JSON! I feel there is also other ways to do this. My problem is i can post data from android to php script to insert data to my server. But what i want to do is get some data from my php to android. (without using JSON). Please, i'm still in the basics. Make this as simple as possible!

下面是我的PHP脚本:

Here's my php script:

<?php
$con=mysqli_connect("HOST", "USER", "PASSWORD", "DB_NAME");

if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$tablenamep = $_POST["tablenamep"];
$stringp = $_POST["stringp"]; 

$val = mysqli_query($con, "DESCRIBE `$tablenamep`");

if($val == TRUE) {
    echo "Table exists";
    $stringp = "This ID already exists. Try again!";
} else {
    echo "Table does not exist";
    mysqli_query($con, "CREATE TABLE ".$tablenamep." ( name VARCHAR(30), number INT, email VARCHAR(30))");
    $stringp = "Your ID is available";
}

mysqli_close($con);
?>

这是我如何用我的Java类POST数据到PHP脚本。

This is how i used my java class to post data to php script.

public void CONNECT_SERVER(){
String msg = etID.getText().toString();

if (msg.length()>0){
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://myfile.php");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("tablenamep", msg));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
} else {
    Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show(); }
}

PHP脚本和Android应用程序工作正常,没有错误!现在,我可以从我的Andr​​oid应用程序的数据添加到PHP脚本。没有问题,到现在为止!

The php script and android app is working FINE, no errors! Now i can add the data from my android app to the php script. NO PROBLEMS TILL NOW!

但我想,是​​在执行脚本之后拿到上面我的Andr​​oid应用程序从PHP脚本$ stringp变量。换句话说,我希望我的应用程序知道ID是否存在。

BUT WHAT I WANT, is to get the $stringp variable from the php script above to my android app after executing the script. In other words i want my app to know whether the ID exists or not.

我已经检查对于这个问题很多论坛。解决这个问题,而JSON。

I have already checked many forums regarding this question. SOLVE THIS PROBLEM WITHOUT JSON.

推荐答案

您必须使用JSON或其他分析方法来检索服务器数据
试试这个

You must use json or other parsing method to retrieve data from server try this

contact.php

contact.php

   <?php

   mysql_connect ("localhost","root","");

    mysql_select_db("meetapp");


    $output=array(); 
     $q=mysql_query("SELECT `app_id` FROM `registration`");

       while($e=mysql_fetch_assoc($q))

        $output[]=$e;

       print (json_encode($output));

        mysql_close();

      ?>

在你的java code

in your java code

                    try {

                    HttpClient httpclient2 = new DefaultHttpClient();
                    HttpPost httppost2 = new HttpPost("http://10.0.2.2:80/contact.php");

                    HttpResponse response2 = httpclient2.execute(httppost2); 
                    HttpEntity entity2 = response2.getEntity();
                    is2 = entity2.getContent();

                    Log.e("log_tag", "connection success ");

            }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection "+e.toString());


                }

                try
                {
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb2 = new StringBuilder();
                        String line = null;
                        while ((line = reader2.readLine()) != null) 
                        {
                                sb2.append(line + "\n");

                        }
                        is.close();

                        result3=sb2.toString();
                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting result "+e.toString());


                }
                try
                {
                    JSONArray jArray2 = new JSONArray(result3);
                    String s11;
                    Log.w("Lengh",""+jArray2.length());
                      for(int i=0;i<jArray2.length();i++){


                        JSONObject json_data2 = jArray2.getJSONObject(i);

                           s11=json_data2.getString("app_id");





                      }  
                }

                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());

                }

这篇关于如何从PHP获取变量到Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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