Skip to content Skip to sidebar Skip to footer

Call Php Function From Android?

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

Solution 1:

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

publicstaticboolean 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(newString(data, 0, len));
                    }

                    inputStream.close();
            }

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


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

                    returntrue;
            } else {

                    returnfalse;
            }

    }

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!

Solution 2:

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.

Solution 3:

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

Post a Comment for "Call Php Function From Android?"