登录后如何启动活动 [英] how to start an activity after my login Activity
本文介绍了登录后如何启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的登录活动中...我要输入用户名和密码..我想检查该用户是否为授权用户..如果该用户已获得授权,则我的网络活动menu.java将启动..else他应该被重定向到相同的登录页面...警告该用户不存在...该怎么办?
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.java starts.. else he should be redirected to the same login page...With a alert that..the user doesn't exist... How to do that??
我已经写了我的活动.如何开始下一个活动?
I have written my activity.. How can I start the next activity??
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login_Menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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);
final HttpClient client = new DefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";
final HttpPost post = new HttpPost(url);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
try{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username",uname.getText().toString()));
pairs.add(new BasicNameValuePair("password",pass.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = null;
while ((line = rd.readLine()) != null) {
tv.append(line);
}
//startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
uname.getText().clear();
pass.getText().clear();
}
});
}
}
推荐答案
在此处使用Aysntask
类
在doInBackground()
中执行哪个运算,并在onPostExecute()
中给出结果
Which do operation in doInBackground()
result and give result in onPostExecute()
public class Login_Menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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);
final HttpClient client = new DefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";
final HttpPost post = new HttpPost(url);
new login().execute("");
}
private class login extends AsyncTask<String, Void, Void>{
ProgressDialog dialog = ProgressDialog.show(activity.this, "", "Loading, Please wait...");
@Override
protected int doInBackground(String... params) {
// TODO Auto-generated method stub
Log.i("thread", "Doing Something...");
//authentication operation
try{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username",uname.getText().toString()));
pairs.add(new BasicNameValuePair("password",pass.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = null;
while ((line = rd.readLine()) != null) {
tv.append(line);
}
//startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
}
catch(Exception e)
{
e.printStackTrace();
}
return val;
}
protected void onPreExecute(){
//dialog.dismiss();
Log.i("thread", "Started...");
dialog.show();
}
protected void onPostExecute(int result){
Log.i("thread", "Done...");
if(dialog!=null)
dialog.dismiss();
if(result){
toast.setText("No User Found, please try again!");
toast.show();
}else{
Intent myIntent = new Intent(ctx, main.class);
myIntent.putExtra("user", user);
startActivity(myIntent);
}
}
}
这篇关于登录后如何启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文