Wolf87
Wolf87

Reputation: 540

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 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.

Upvotes: 4

Views: 11342

Answers (3)

N3sh
N3sh

Reputation: 878

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!

Upvotes: 4

elo
elo

Reputation: 615

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

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

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.

Upvotes: 1

Related Questions