简体   繁体   中英

How to perform simple Http Post in android?

I'm new to http Post. All i want to do is to send that access_id=44321 as a url like http://myurl.com/access_id=44321 will insert 44321 to access_id in the database how to perform this operation. Am I doing it Right ?

Thanks for the help !

public class IvrsPushService {
    URL url;
    HttpURLConnection conn;
Details details;

    String userId;


    void pushData() throws Exception {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://myurl.com/Acces/DEFAULT2.ASPX?");

        try {
            String accessid=Details.getAssetid();
            String userid=Details.getUserid();
            String datetime=Details.getDate()+"\""+Details.getTime();
            String mobilenumber="9785";


            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("access", accessid));
            nameValuePairs.add(new BasicNameValuePair("user", userid));
            nameValuePairs.add(new BasicNameValuePair("date", datetime));
            nameValuePairs.add(new BasicNameValuePair("mobilenumber", mobilenumber));
            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
        }
        }


    }

Http Client from Apache Commons is the way to go. It is already included in android. Here's a simple example of how to do HTTP Post using it.

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
}

}

Try this
HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://myurl.com/");
         try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("id", "44325"));
                nameValuePairs.add(new BasicNameValuePair("first_name", "abc"));
                nameValuePairs.add(new BasicNameValuePair("last_name", "xyz"));
                nameValuePairs.add(new BasicNameValuePair("location", "lmn"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
            } catch (Exception e) {

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM