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) {
        }


No comments:

Post a Comment