Sending data such as integers and text strings from a phone to a web database

前端 未结 2 1076
迷失自我
迷失自我 2021-01-23 20:51

I have a project where I\'m supposed to send data such as integers, floats and text strings from an android app to a web database. However I don\'t have the first clue on how to

相关标签:
2条回答
  • 2021-01-23 21:26
    1. You need program some server-side logic (PHP page that accepting parameters key=value by POST or GET method)
    2. Then if data is verified save it to the database
    3. In phone you must implement HttpClient and HttpPost classes to POST this data to PHP page

    In phone you can use following code (no tested):

        public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
    
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
    
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } 
    

    In PHP you can do something like this:

    <?php
    //Check whether the data has been submitted
    if (isset($_POST['id'] && isset($_POST['stringdata'])) ) {
    
       //Let's now print out the received values in the browser
       echo "Id: {$_POST['id']}<br />";
       echo "String data: {$_POST['stringdata']}<br />";
    
       //you can implement database logic here too (insert data to database)
    } else {
        echo "You can't see this page without submitting the data.";
    }
    ?>
    
    0 讨论(0)
  • 2021-01-23 21:47

    Take a look on soap (on android you can use the ksoap2 package) you cen also create a socket connection to o program on the server side

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