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.

3 Answers 3

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!

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

4 Comments

let me know if the php is also needed ;)
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.
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 ;) stackoverflow.com/questions/10186266/…
@N3sh what packages i need to import for this ?
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.

2 Comments

Do you mean something like this $sql=mysql_query("INSERT INTO CITY (CITY_NAME)VALUES('".$_REQUEST['c_name']."')");
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
1

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

Comments

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.