Apr 6, 2011

Executing a HTTP POST Request with HttpClient

//  here i am explaining how to send some data to server

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class SendInfo {
    private boolean isPosted = false;

    public SendInfo() {
    }

    public boolean PostData() {
        try {
  
            HttpClient mClient = new DefaultHttpClient();
            HttpPost mpost = new HttpPost("http://www.webimm.com/");
         List<NameValuePair> nameValuepairs = new ArrayList<NameValuePair>(2);
            nameValuepairs.add(new BasicNameValuePair("101",
                    "sravankumar@vizser.com"));
            Log.w("send info1 ", "send info1");
            nameValuepairs.add(new BasicNameValuePair("102",
                    "suryaprakash@vizser.com"));
            Log.w("send info2 ", "send info2");
            mpost.setEntity(new UrlEncodedFormEntity(nameValuepairs));
            HttpResponse responce = mClient.execute(mpost);
            HttpEntity entity = responce.getEntity();
           BufferedReader buf = new BufferedReader(new InputStreamReader(
                    entity.getContent()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = buf.readLine()) != null) {
                Log.w(" data is ", line);
            }
            isPosted = true;
            return isPosted;
        } catch (UnsupportedEncodingException e) {
         
            Log.w(" error ", e.toString());
        } catch (Exception e) {

            Log.w(" error ", e.toString());
        }

        return isPosted;

    }

}


No comments:

Post a Comment