-2

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.

3
  • Please note that tags are not keywords. Stuffing the tag list full of the same words that are in your question (app, get, variables) will not help categorize it. Always be sure to read the descriptions that appear when selecting tags! Commented Apr 11, 2014 at 8:56
  • Do you search before posting? androidhive.info/2012/05/how-to-connect-android-with-php-mysql Commented Apr 11, 2014 at 8:57
  • Also, you have an SQL injection vulnerability in your code - you're passing raw user input in as a table name. Because MySQL doesn't do placeholders and prepared statements when the thing being placeheld is an identifier, you must perform format validation yourself by hand. Commented Apr 11, 2014 at 8:57

1 Answer 1

0

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

                }
Sign up to request clarification or add additional context in comments.

3 Comments

Not happy with the answer. Again JSON. ok i get it. JSON is the only method to get data from php. But the code above contains many errors. I dont exactly get which data your trying to get. Anyway thanks for the answer!
you need to declare all string variable globally, i think that is the error
Hey i tried your answer. but nothing happens! When i want to add some data to a php variable (eg- $tablename), so can i use jason_data2.put("tablename", msg); I tried this code but doesnt work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.