• Home
  • About me

Rahul Upadhyay's Blog

Place of Knowledge…
Stay updated via RSS

  • about.me

    Rahul Upadhyay

    Rahul Upadhyay

    Android Developer

  • Recent Posts

    • Cal-Clock (A DayDream Application)
    • Cool stuff with Android Interface Definition Language (AIDL) :)
    • Facebook SSO, Generating HASH KEY
    • How to generate and use Google MAP API 2
    • WebResponce in Android
  • Archives

    • May 2013
    • April 2013
    • March 2013
    • February 2013
    • July 2012
    • June 2012
    • May 2012
    • January 2012
  • Categories

    • Android
    • Knowledge
    • Technology
  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 30 other followers

  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com

HttpClient in Android

Posted: July 31, 2012 in Android
Tags: Android, HttpClient, HttpPost, Login
0

Hello Droids..!

Here I come with another demo for HttpClient, in which you have to do login to URL.

The core is used “HttpPost”,

 

Here is the code,

</pre>
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

&nbsp;
public class Login {

 public String HttpClient(String UserName,String Password){

 String response;
 try {

 DefaultHttpClient httpClient = new DefaultHttpClient();
 ResponseHandler <String> resonseHandler = new BasicResponseHandler();
 HttpPost postMethod = new HttpPost(your URL); //without parameters
 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

 //nameValuePairs.add(new BasicNameValuePair("api", "Login"));// If you get an error then and then active this line
 nameValuePairs.add(new BasicNameValuePair("username", UserName));
 nameValuePairs.add(new BasicNameValuePair("password", Password));

 postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 response = httpClient.execute(postMethod,resonseHandler);

 } catch (Exception e) {

 e.printStackTrace();
 }
 return response;

 }
 }

How to use:

//Create an object
Login login = new Login();
String resp = login.HttpClient(abc,pqr); //here abc -> UserName, pqr -> password

Download here:: Login.java

Comments will be appreciated. :)

About these ads

Share this:

  • Facebook
  • Google +1
  • Twitter

Like this:

Like Loading...

Leave a Reply Cancel reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. ( Log Out / Change )

Twitter picture

You are commenting using your Twitter account. ( Log Out / Change )

Facebook photo

You are commenting using your Facebook account. ( Log Out / Change )

Cancel

Connecting to %s

Android Facebook Demo
WebResponce in Android

Blog at WordPress.com. | Theme: Greyzed by The Forge Web Creations.
Follow

Get every new post delivered to your Inbox.

Join 30 other followers

Powered by WordPress.com
%d bloggers like this: