Call PHP function from android?

后端 未结 3 943
遇见更好的自我
遇见更好的自我 2020-12-10 21:03

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 e

相关标签:
3条回答
  • 2020-12-10 21:22

    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.

    0 讨论(0)
  • 2020-12-10 21:25

    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!

    0 讨论(0)
  • 2020-12-10 21:25

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

    0 讨论(0)
提交回复
热议问题