Tuesday, December 14, 2010

AsyncTask With HttpClient in Android

Asynctask with HttpClient in android












Complete Source Code:


Activity Code:
loginPage.java


public class LoginPage extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.login_page);
txtbox1= (EditText) findViewById(R.id.userName);
button1 = (Button) findViewById(R.id.login);
tv = (TextView) findViewById(R.id.Validation);
txtbox2= (EditText) findViewById(R.id.Password);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;

a = txtbox1.getText().toString();
b = txtbox2.getText().toString();

if(a.equals(b))
    reportview();

else
    tv.setText("check U'r Passwor");


}

private Object clear() {
    // TODO Auto-generated method stub
    return null;
}
}


private void reportview(){
   
   
   
    Intent reportintent = new Intent( this, httpclient.class);
       Activity parentAct = (Activity)this;
   
       parentAct.startActivity(reportintent);
}
}            




HttpClient.java



public class httpclient extends Activity { 
    private Object GetURL;

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
   
         TextView tv = new TextView(this);
         this.GetURL=GetURL;
   
         setContentView(tv);
        getURL("http://www.google.co.in/"); 
    } 
     
    public void getURL(String url) { 
        new GetURL().execute(url); 
    } 
     
    private class GetURL extends AsyncTask<String, Void, Void> { 
        private final HttpClient Client = new DefaultHttpClient(); 
        private String Content; 
        private String Error = null; 
        private ProgressDialog Dialog = new ProgressDialog(httpclient.this); 
         
        protected void onPreExecute() { 
            Dialog.setMessage("loading source.."); 
         
            Dialog.setTitle("vijay");
            Dialog.show(); 
        } 
 
        protected Void doInBackground(String... urls) { 
            try { 
                HttpGet httpget = new HttpGet(urls[0]); 
                ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
                Content = Client.execute(httpget, responseHandler); 
            } catch (ClientProtocolException e) { 
                Error = e.getMessage(); 
                cancel(true); 
            } catch (IOException e) { 
                Error = e.getMessage(); 
                cancel(true); 
            } 
             
            return null; 
        } 
         
        protected void onPostExecute(Void unused) { 
            Dialog.dismiss(); 
            if (Error != null) { 
                Toast.makeText(httpclient.this, "Wrong Url", Toast.LENGTH_LONG).show(); 
            } else { 
                Toast.makeText(httpclient.this, "Source: " + Content, Toast.LENGTH_LONG).show(); 
            } 
        } 
         
    } 

login_page.xml


 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
  
  
      
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:paddingTop="100dip"
        android:background="#000000"
        android:layout_height="fill_parent">
        <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                 android:paddingTop="6dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:background="#000000"
                android:layout_height="wrap_content">
      
      
      
            <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                 android:paddingTop="6dip"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:background="#000000"
                android:layout_height="wrap_content">  
      
  
                       
                <TableLayout android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:gravity="center_horizontal"
                    android:background="#000000"
                    android:layout_height="wrap_content" android:padding="5dip">
        <TableRow android:paddingTop="25dip">
      
                <TextView
            
             android:id="@+id/userNameText" 
             android:layout_height="fill_parent"
            android:textColor="#e7a71c"
            android:textSize="7pt"
            android:textStyle="bold"
            android:paddingLeft="8dip"
            android:text="User Name:"
             android:layout_span="2"
            android:layout_width="fill_parent"                                                                                                
            android:gravity="left"
            />
      
        <EditText
            android:id="@+id/userName"
          
            android:layout_width="144px"
            android:layout_height="40px"
            android:text=""
            android:singleLine="True"></EditText>
      
      
        </TableRow>
      
        <TableRow>
      
                <TextView
            
            android:id="@+id/PasswordText" 
             android:layout_height="fill_parent"
            android:textColor="#e7a71c"
            android:textSize="7pt"
            android:textStyle="bold"
            android:paddingLeft="8dip"
            android:text="Password:"
             android:layout_span="2"
            android:layout_width="fill_parent"                                                                                                
            android:gravity="left"
            />
      
        <EditText
            android:id="@+id/Password"
            android:password="true"
            android:layout_width="144px"
            android:layout_height="40px"
            android:text="Password"
            android:singleLine="True"></EditText>
      
      
        </TableRow>
      
        <TableRow android:paddingLeft="80dip"
                  android:paddingTop="10dip">
        <Button
         android:id="@+id/login"
         android:gravity="center"
       
         android:layout_width="fill_parent"
         android:layout_height="40px"
         android:text="Log In"/>
       </TableRow>
<TableRow>
<TextView
android:id="@+id/Validation"
android:layout_width="115dip"
android:layout_height="wrap_content"

android:textStyle="bold"

android:layout_x="5px"
android:layout_y="150px"
></TextView>



                            
                </TableRow>                                                                                                                                                                                                                                                                              
        </TableLayout>
      
      

</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...