0

I was searching for a way to send a data from android app to PHP and display it in an HTML. I saw the same question and try it How to display data sent from android app to a php on an html page?. But now it does not work.

My android app feature is to input firstname, lastname and address. After completing the input, click button save and then redirect to the web browser and display the input details. This is what I've tried.

Android App

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        savebtn = findViewById(R.id.savebtn);
        editTextFName   = findViewById(R.id.editTextFName);
        editTextLName   = findViewById(R.id.editTextLName);
        editTextAddress = findViewById(R.id.editTextAddress);

        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String val_Fname = editTextFName.getText().toString();
                String val_Lname = editTextLName.getText().toString();
                String val_Addr = editTextAddress.getText().toString();

                HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx/webdisplay/index.php");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                
                try{
                    nameValuePairs.add(new BasicNameValuePair("android_Fname", val_Fname);
                    nameValuePairs.add(new BasicNameValuePair("android_Lname", val_Lname));
                    nameValuePairs.add(new BasicNameValuePair("android_Addr", val_Addr));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = httpclient.execute(httppost);
                }catch(Exception e){
                    Log.e("log_tag", "Error"+e.toString());
                }

                redirectTo("http://xxx.xxx.xxx.xxx/webdisplay/index.php");

            }
        });
    }

PHP

<?php
    $dataFname = $_GET['android_Fname'];
    $dataLname = $_GET['android_Lname'];
    $dataAddr = $_GET['android_Addr'];
?>

<html>
<body>

<div>
    <?php echo $dataFname; ?>
    <?php echo $dataLname; ?>
    <?php echo $dataAddr; ?>
</div>

</body>
</html> 

Reference: https://www.folkstalk.com/tech/how-to-display-data-sent-from-android-app-to-a-php-on-an-html-page-solutions/

16
  • Your question was already answered in this forum [Forum][1] I hope it may help you [1]: folkstalk.com/tech/… Commented Dec 1, 2022 at 14:40
  • and then redirect to the web browser and display the input details. That is not possible with another app or with another call to the same url as then you get the html form elements empty just like when you called it for the first time. (Why didn't you tell?). No. Your page is in HttpResponse response. You could display it in a WebView. Commented Dec 1, 2022 at 14:41
  • 1
    @Ademkriouane Thank you but it doesn't work too. It is the same answer on this post stackoverflow.com/questions/18328663/… Commented Dec 1, 2022 at 14:44
  • 1
    you seem to send POST request yet you try to read $_GET instead of $_POST Commented Dec 1, 2022 at 14:47
  • @MarcinOrlowski I tried to change it now to $_POST but it is still empty no data is showing Commented Dec 1, 2022 at 14:58

0

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.