4

I want to call specific php function on server from Android application and also to send some parameters. Till now I achieved that I can open php file using HttpClient and executed data transfer to Json and show that in my app. So, now I want to be able to call specific function and send parameter to it, how can I do that?? Thanks.

Wolf87
  • 546
  • 3
  • 11
  • 29

3 Answers3

4

Here is a piece of code I wrote for registering a new username using JSON:

    public static boolean register(Context myContext, String name, String pwd) {

            byte[] data;
            HttpPost httppost;
            StringBuffer buffer;
            HttpResponse response;
            HttpClient httpclient;
            InputStream inputStream;
            List<NameValuePair> nameValuePairs;

            try {
                    httpclient = new DefaultHttpClient();
                    httppost = new HttpPost(
                                    "http://X.X.X.X/register.php");
                    // Add your data
                    nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("User", name.trim()));
                    nameValuePairs.add(new BasicNameValuePair("Password", pwd.trim()));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    response = httpclient.execute(httppost);
                    inputStream = response.getEntity().getContent();

                    data = new byte[256];

                    buffer = new StringBuffer();
                    int len = 0;
                    while (-1 != (len = inputStream.read(data))) {
                            buffer.append(new String(data, 0, len));
                    }

                    inputStream.close();
            }

            catch (Exception e) {
                    Toast.makeText(myContext, "error" + e.toString(), Toast.LENGTH_LONG)
                                    .show();
                    return false;
            }


            if (buffer.charAt(0) == 'Y') {

                    return true;
            } else {

                    return false;
            }

    }

If you notice:

            nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("User", name.trim()));
                    nameValuePairs.add(new BasicNameValuePair("Password", pwd.trim()));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

in that piece you can send parameters.

The method simply sends User and Password to the register.php. If the User is already taken, 'N' is returned; otherwise creates the User and returns 'Y'.

On the server side, you treat them as POST information, so:

 $user = $_POST['User'];

It should do for your case :)

Cheers!

gprathour
  • 14,313
  • 5
  • 60
  • 89
N3sh
  • 870
  • 8
  • 34
  • let me know if the php is also needed ;) – N3sh Apr 25 '12 at 10:05
  • Thank you so much! Just one more question, can I call for example php function "function getSettings($value)" which will send something back to Android? Because I want few functions in the same php file, and to call them separately when I need. Is there command for that? Thank you one more time. – Wolf87 Apr 25 '12 at 10:11
  • If you see there, it already returns something (either 'Y' or 'N'). So, if it is something simple like a char, a string etc.. you can just read the buffer and 'build' the response and use it accordingly. Otherwise you need to do something extra: Check this question of mine, it will be helpful ;) http://stackoverflow.com/questions/10186266/android-json-mysql-query-gives-networkonmainthreadexception – N3sh Apr 25 '12 at 10:15
  • @N3sh what packages i need to import for this ? – k_kumar Mar 07 '19 at 14:39
1

Present or wrap-up those specific php functions with in a web-service together with your required parameters and call that web-service including the input parameters from your android to get things done.

waqaslam
  • 66,380
  • 16
  • 161
  • 174
  • Do you mean something like this $sql=mysql_query("INSERT INTO CITY (CITY_NAME)VALUES('".$_REQUEST['c_name']."')"); – Wolf87 Apr 25 '12 at 09:42
  • are asking to send whole query as parameter? if yes, then its wrong. You should include the query part in web-service. and only send city name as in parameters to the web-service from phone – waqaslam Apr 25 '12 at 09:53
1

You need to use WebServices for this work. http://www.php.net/manual/en/book.soap.php

elo
  • 615
  • 4
  • 11