I want to send data to my php page using android. How can I do it?
            Asked
            
        
        
            Active
            
        
            Viewed 3.7k times
        
    3 Answers
12
            
            
        The Android API has a set of functions that allows you to use HTTP requests, POST, GET etc. In this example i will provide a set of code that will allow you to update the contents of a file in a server using POST requests.
Our server side code will be very simple and it will be written in PHP. The code will get data from a post request, update a file with the data and load this file to display it in a browser.
Create PHP page on server "mypage.php", the code for php page is:-
 <?php
 $filename="datatest.html";
 file_put_contents($filename,$_POST["fname"]."<br />",FILE_APPEND);
 file_put_contents($filename,$_POST["fphone"]."<br />",FILE_APPEND);
 file_put_contents($filename,$_POST["femail"]."<br />",FILE_APPEND);
 file_put_contents($filename,$_POST["fcomment"]."<br />",FILE_APPEND);
 $msg=file_get_contents($filename);
 echo $msg; ?>
Create Android Project and write below code in HTTPExample.java
           HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://example.com/mypage.php");
         try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
       nameValuePairs.add(new BasicNameValuePair("fname", "vinod"));
       nameValuePairs.add(new BasicNameValuePair("fphone", "1234567890"));
       nameValuePairs.add(new BasicNameValuePair("femail", "abc@gmail.com"));
       nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       httpclient.execute(httppost);
     } catch (ClientProtocolException e) {
         // TODO Auto-generated catch block
     } catch (IOException e) {
         // TODO Auto-generated catch block
     }
Add permission in AndroidManifest.xml
    <uses-permission android:name="android.permission.INTERNET"/>
 
    
    
        vinsoft
        
- 131
- 1
- 4
- 
                    i have Do with ur code there are problem unfortunately closed i have completely insert permit-ion of internet....can u add more in your code ....why i getting error and values are not going to server.... – Amitsharma Feb 18 '14 at 10:34
6
            You can make GET or POST requests using AndroidHttpClient:
- Create an AndroidHttpClient to execute your requests.
- Create either an HttpGet or HttpPost request.
- Use setEntity and setHeader methods to populate the request.
- Use one of the execute methods on your client with your request.
This answer seems like a fairly complete code sample.
4
            
            
        A quick example for a HTTP POST request is given here:
try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    // Send data
    URL url = new URL("http://hostname:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
}
 
     
     
     
     
    