Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

How to get a variable from php to android?

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!

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);
?>

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

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!

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

      ?>

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 + "
");

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

                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...