如何从php到android获取变量


How to get a variable from php to android?

我一直在到处搜索这个问题,我看到的唯一答案是JSON!我觉得还有其他方法可以做到这一点。我的问题是,我可以将数据从android发布到php脚本,将数据插入我的服务器。但我想做的是从我的php中获取一些数据到android。(不使用JSON)。求你了,我还处于基础阶段。让这件事尽可能简单!

这是我的php脚本:

<?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类将数据发布到php脚本的方式。

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应用程序运行良好,没有错误!现在我可以将我的android应用程序中的数据添加到php脚本中。到目前为止没有问题!

但我想要的是,在执行脚本后,将$stringp变量从上面的php脚本中获取到我的android应用程序中。换句话说,我想让我的应用程序知道ID是否存在。

关于这个问题,我已经查看了很多论坛。不用JSON就可以解决这个问题。

您必须使用json或其他解析方法从服务器检索数据试试这个

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代码中

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