Friday, February 26, 2010

Http client API Post method implementation:

Android client side
1.create simple activity with edit text.
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
        httpHelper = new HttpHelper();
        etContents =(EditText) findViewById(R.id.etContents);
    }


2.Create Name Value Pair add parameters user name and  password. 

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userName", "nava"));
            nameValuePairs.add(new BasicNameValuePair("password", "nav"));
            HttpResponse response = httpHelper.doPost("URLHERE", nameValuePairs);
           

           
3.Create http post method and set header and entity. Create Default Http Client,Http Context and execute the  http request.

DefaultHttpClient httpClient;
HttpContext localContext;
HttpPost request = new HttpPost(url);
        HttpResponse response = null;   
        Log.d("Service Call ",url);
        try {
            // Set parameters
            if (parameters != null) {
                request.setHeader("Content-Type", "application/x-www-form-urlencoded");
                request.setEntity(new UrlEncodedFormEntity(parameters));
            }           
            response = httpClient.execute(request, localContext);
           
           
        } catch (ClientProtocolException e) {
            // put your code here
        } catch (IOException e) {
            // put your code here
        }

4.Get the response from the server and display the content to edit text

try {               
                InputStream instream = response.getEntity().getContent();
                content = httpHelper.inconvertibility(instream);
                etContents.setText(content);
            } catch (IllegalStateException e1) {               
                e1.printStackTrace();
            } catch (IOException e1) {               
                e1.printStackTrace();
            }


Server side implementation:

1.create simple web application and that application receive the request parameter.

 public void crmLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String userName = "";
        String password = "";

        String result = "";

      
        if (request.getParameter("userName") != null) {
            userName = request.getParameter("userName");
        }

        if (request.getParameter("password") != null) {
            password = request.getParameter("password");
        }

2.get the user name and password from the request and set the user name and password to response.

result="userNamee="+userName+""+"password="+password;

        response.setContentType("text/plain");
        response.setHeader("Cache-Control", "no-cache");
        try {
            response.getWriter().write(result);
        } catch (Exception e) {
        }


4 comments:

  1. Hello.

    The class HttpHelper is not a class of the Android API. Could you show this class?

    Thanks.

    ReplyDelete
  2. Hey can you please show implementation of HttpHelper class.

    ReplyDelete
  3. what is HttpHelper ? and how to overcome this error.

    ReplyDelete
  4. Earn Rs.25000/- per month - Simple online Jobs - Are You Looking for Home-Based Online Jobs? - Are You a Student, Housewife, jobseeker ? - Are you ready to Work 1 to 2 Hours daily Online? - Do You need Guaranteed Payment Monthly? Then this is for You, - Clicking on their Advertisement E-mails. - Submitting their Data\'s online. - Reading their Advertisement Sms. - Filling Forms on their websites, etc,. FREE to Join >> http://dailyonlinejobs.com
    9PJK1587500784 2020-04-23 21:45:00

    ReplyDelete