Tuesday, 12 August 2014

how to use http post method in android


call following method

executeMultipartPost("username","password","https://your post url");

use this method

 public static void executeMultipartPost(String username, String password, String url) {

            try {

                HttpClient client = new DefaultHttpClient();
                HttpPost poster = new HttpPost(url);

                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                entity.addPart("username", new StringBody(username));

                entity.addPart("password", new StringBody(password));

                 poster.setEntity(entity);

                client.execute(poster, new ResponseHandler<Object>() {
                   
                public Object handleResponse(HttpResponse response)throws ClientProtocolException, IOException {

                        HttpEntity respEntity = response.getEntity();
                        String jsonData = EntityUtils.toString(respEntity);
                       
                        Log.d("Response:json", jsonData);
               
                       return null;
                    }
               
                });
            } catch (Exception e) {
                // do something with the error
                System.out.println("!!!!!!!!!!!!"+e);
            }
        }

Friday, 1 August 2014

how to post method request in android example


public void postRequest() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("Your URL Type");
    try
{       
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "123"));
        nameValuePairs.add(new BasicNameValuePair("name", "Hiiii"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        Log.i("Response---",response );
    } catch (ClientProtocolException e)
 {
        // TODO Auto-generated catch block
    }
catch (IOException e)
{
        // TODO Auto-generated catch block
    }
}