在我的登录活动中...我在询问用户名和密码..我想检查用户是否是授权用户..如果用户已获得授权,那么我的网络活动menu.java会启动..否则他应该被重定向到同一个登录页面......有一个警告......用户不存在......怎么做?
我写了我的活动..我怎样才能开始下一个活动?
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();
}
});
}
}
答案 0 :(得分:2)
在此处使用Aysntask
课程
doInBackground()
中的哪个操作会导致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);
}
}
}
答案 1 :(得分:1)
使用Aysntask
在按钮点击时创建HttpPost
。启动新活动时,它取决于用户成功授权后返回的服务。例如
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;
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=new Intent(CurrentActivity.this,DEMO_RETRV.this);
startActivity(intent);
//startActivity(new Intent("com.campuspro.start.DEMO_RETRV"));
}
else
{
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
答案 2 :(得分:0)
在线程中建立http连接。
并在UI-Handler中发送响应
像
//UI-Handler
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch(msg.what){
case 100:
String response = (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(new View.OnClickListener() {
public void onClick(View arg0) {
Thread t = new Thread()
{
public void run()
{
try{
final HttpClient client = new DefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";
final HttpPost post = new HttpPost(url);
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);
StringBuffer sb = new StringBuffer()
while ((line = rd.readLine()) != null) {
sb.append(line);
}
//send the response in a UI-Handler
Message message = new Message();
message.obj=sb.toString();
message.what=100;
handler.sendMessage( message );
}catch(Exception e){}
}
}
t.start();
}
答案 3 :(得分:0)
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;
public class Login_Menu extends Activity {
EditText usname;
EditText pass;
TextView tv;
HttpClient client;
HttpPost post;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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 = new DefaultHttpClient();
String url="http://10.0.2.2:7001/proj/login.jsp";
post = new HttpPost(url);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new login().execute("");
}
});
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
usname.getText().clear();
pass.getText().clear();
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
private class login extends AsyncTask<String, Void, JSONObject>{
ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating, Please wait...");
@Override
protected JSONObject 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",usname.getText().toString()));
pairs.add(new BasicNameValuePair("password",pass.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(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=new JSONObject(data);
return last;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPreExecute(){
//dialog.dismiss();
Log.i("thread", "Started...");
dialog.show();
}
protected void onPostExecute(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=new Bundle();
newbundle.putString("uname",name);
Intent myIntent=new Intent(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();
}
}
}}