How To Start An Activity After My Login Activity
In my login activity...I am asking the username and password.. I want to check whether the user is a authorized user or not.. If the user is authorized then my net activity menu.ja
Solution 1:
Use Aysntask class over here
Which do operation in doInBackground() result and give result in onPostExecute()
publicclassLogin_MenuextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.login_lay);
final TextView tv=(TextView) findViewById(R.id.login_stat_tv);
final EditText uname=(EditText)findViewById(R.id.uname);
final EditText pass=(EditText)findViewById(R.id.pass);
Button login=(Button)findViewById(R.id.login_but);
Button cancel=(Button)findViewById(R.id.cancel_but);
finalHttpClientclient=newDefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";
finalHttpPostpost=newHttpPost(url);
newlogin().execute("");
}
privateclassloginextendsAsyncTask<String, Void, Void>{
ProgressDialogdialog= ProgressDialog.show(activity.this, "", "Loading, Please wait...");
@OverrideprotectedintdoInBackground(String... params) {
// TODO Auto-generated method stub
Log.i("thread", "Doing Something...");
//authentication operationtry{
List<NameValuePair> pairs = newArrayList<NameValuePair>();
pairs.add(newBasicNameValuePair("username",uname.getText().toString()));
pairs.add(newBasicNameValuePair("password",pass.getText().toString()));
post.setEntity(newUrlEncodedFormEntity(pairs));
HttpResponseresponse= client.execute(post);
BufferedReaderrd=newBufferedReader(newInputStreamReader(
response.getEntity().getContent()));
Stringline=null;
while ((line = rd.readLine()) != null) {
tv.append(line);
}
//startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
}
catch(Exception e)
{
e.printStackTrace();
}
return val;
}
protectedvoidonPreExecute(){
//dialog.dismiss();
Log.i("thread", "Started...");
dialog.show();
}
protectedvoidonPostExecute(int result){
Log.i("thread", "Done...");
if(dialog!=null)
dialog.dismiss();
if(result){
toast.setText("No User Found, please try again!");
toast.show();
}else{
IntentmyIntent=newIntent(ctx, main.class);
myIntent.putExtra("user", user);
startActivity(myIntent);
}
}
}
Solution 2:
use Aysntask for making HttpPost on button click.for starting new Activity it's depend on what your servce is returning when user successfully authorized.like
publicvoidonClick(View arg0) {
try{
List<NameValuePair> pairs = newArrayList<NameValuePair>();
pairs.add(newBasicNameValuePair("username",uname.getText().toString()));
pairs.add(newBasicNameValuePair("password",pass.getText().toString()));
post.setEntity(newUrlEncodedFormEntity(pairs));
HttpResponseresponse= client.execute(post);
BufferedReaderrd=newBufferedReader(newInputStreamReader(
response.getEntity().getContent()));
Stringline=null;
String strinfo="";
while ((line = rd.readLine()) != null) {
tv.append(line);
strinfo+=line;
}
//check here authorized user or not
strinfo = strinfo.toLowerCase();
String strlogin="login=true";
if((strinfo.contains(strlogin)==true)
{
Intent intent=newIntent(CurrentActivity.this,DEMO_RETRV.this);
startActivity(intent);
//startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
}
else
{
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
Solution 3:
make http connection in a thread.
and send the response in a UI-Handler
like
//UI-HandlerfinalHandlerhandler=newHandler()
{
publicvoidhandleMessage(Message msg)
{
switch(msg.what){
case100:
Stringresponse= (String)msg.obj;
tv.setText(response);
}
}
}
//make Http Connection inside button's onclick and send http response to handleMessage(Message msg) method to update UI
login.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View arg0) {
Threadt=newThread()
{
publicvoidrun()
{
try{
finalHttpClientclient=newDefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";
finalHttpPostpost=newHttpPost(url);
List<NameValuePair> pairs = newArrayList<NameValuePair>();
pairs.add(newBasicNameValuePair("username",uname.getText().toString()));
pairs.add(newBasicNameValuePair("password",pass.getText().toString()));
post.setEntity(newUrlEncodedFormEntity(pairs));
HttpResponseresponse= client.execute(post);
StringBuffersb=newStringBuffer()
while ((line = rd.readLine()) != null) {
sb.append(line);
}
//send the response in a UI-HandlerMessagemessage=newMessage();
message.obj=sb.toString();
message.what=100;
handler.sendMessage( message );
}catch(Exception e){}
}
}
t.start();
}
Solution 4:
package com.campuspro.start;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
publicclassLogin_MenuextendsActivity {
EditText usname;
EditText pass;
TextView tv;
HttpClient client;
HttpPost post;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.login_lay);
tv=(TextView) findViewById(R.id.login_stat_tv);
usname=(EditText)findViewById(R.id.uname);
pass=(EditText)findViewById(R.id.pass);
Button login=(Button)findViewById(R.id.login_but);
Button cancel=(Button)findViewById(R.id.cancel_but);
client = newDefaultHttpClient();
String url="http://10.0.2.2:7001/proj/login.jsp";
post = newHttpPost(url);
login.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View arg0) {
// TODO Auto-generated method stubnewlogin().execute("");
}
});
cancel.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View v) {
// TODO Auto-generated method stub
usname.getText().clear();
pass.getText().clear();
}
});
}
@OverrideprotectedvoidonDestroy() {
// TODO Auto-generated method stubsuper.onDestroy();
}
@OverrideprotectedvoidonPause() {
// TODO Auto-generated method stubsuper.onPause();
finish();
}
privateclassloginextendsAsyncTask<String, Void, JSONObject>{
ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating, Please wait...");
@OverrideprotectedJSONObjectdoInBackground(String... params) {
// TODO Auto-generated method stubLog.i("thread", "Doing Something...");
//authentication operationtry{
List<NameValuePair> pairs = newArrayList<NameValuePair>();
pairs.add(newBasicNameValuePair("username",usname.getText().toString()));
pairs.add(newBasicNameValuePair("password",pass.getText().toString()));
post.setEntity(newUrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
int status=response.getStatusLine().getStatusCode();
if(status == 200)
{
HttpEntity e=response.getEntity();
String data=EntityUtils.toString(e);
JSONObject last=newJSONObject(data);
return last;
}
}
catch(Exception e)
{
e.printStackTrace();
}
returnnull;
}
protectedvoidonPreExecute(){
//dialog.dismiss();Log.i("thread", "Started...");
dialog.show();
}
protectedvoidonPostExecute(JSONObject result){
Log.i("thread", "Done...");
String status;
String name;
try {
status= result.getString("status");
name=result.getString("uname");
if(dialog!=null)
{
dialog.dismiss();
}
if(status.equalsIgnoreCase("yes"))
{
tv.setText("Login Successful...");
Bundle newbundle=newBundle();
newbundle.putString("uname",name);
Intent myIntent=newIntent(Login_Menu.this,Instruction.class);
myIntent.putExtras(newbundle);
startActivity(myIntent);
}else{
tv.setText("No User Found, please try again!");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
Post a Comment for "How To Start An Activity After My Login Activity"