Android 登录应成功,但未链接到我的主菜单界面
这是我的日志Android 登录应成功,但未链接到我的主菜单界面,android,json,Android,Json,这是我的日志 12-08 14:45:21.179: D/request!(6046): starting 12-08 14:45:21.719: D/Login attempt(6046): {"message":"Login successful!","success":1} 这是我的Login.java public class Login extends ActionBarActivity implements OnClickListener { private Button lo
12-08 14:45:21.179: D/request!(6046): starting
12-08 14:45:21.719: D/Login attempt(6046): {"message":"Login successful!","success":1}
这是我的Login.java
public class Login extends ActionBarActivity implements OnClickListener {
private Button login,register;
private EditText email,password;
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.14:1234/PMSS/login.php";
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.registerlauncher);
email = (EditText) findViewById(R.id.userid);
password = (EditText) findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Username = email.getText().toString();
String Password = password.getText().toString();
new AttemptLogin(Username,Password).execute();
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(Login.this, Register.class);
startActivity(i);
break;
*/
default:
break;
}
}
//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {
//three methods get called, first preExecture, then do in background, and once do
//in back ground is completed, the onPost execture method will be called.
boolean failure = false;
String res;
String Username;
String Password;
int success;
public AttemptLogin(String Username, String Password) {
this.Username = Username;
this.Password = Password;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
params.add(new BasicNameValuePair("password", Password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
System.out.print("Here");
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
res = json.getString(TAG_MESSAGE);
return res;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(int success) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (success == 1) {
Log.d("Login Successful!", res);
Intent i = new Intent(Login.this, MainMenu.class);
startActivity(i);
Toast.makeText(Login.this, res, Toast.LENGTH_LONG).show();
}else{
Log.d("Login Failure!", res);
Toast.makeText(Login.this, res, Toast.LENGTH_LONG).show();
}
}
}
}
公共类登录扩展ActionBarActivity实现OnClickListener{
私有按钮登录、注册;
私人编辑文本电子邮件,密码;
//进度对话框
私人对话;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串登录\u URL=”http://192.168.1.14:1234/PMSS/login.php";
//php脚本repsonse中的JSON元素ID:
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@SuppressLint(“新API”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
login=(按钮)findviewbyd(R.id.login);
寄存器=(按钮)findviewbyd(R.id.registerlauncher);
email=(EditText)findViewById(R.id.userid);
密码=(EditText)findViewById(R.id.password);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
字符串Username=email.getText().toString();
字符串密码=Password.getText().toString();
新建AttemptLogin(用户名、密码).execute();
}
});
register.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
意图=新意图(Login.this、Register.class);
星触觉(意向);
}
});
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.HONEYCOMB){
//对于主要活动,请确保操作栏中的应用程序图标
//不起按钮的作用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.login,menu);
返回true;
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
/*案例R.id.login:
新建AttemptLogin().execute();
打破
案例R.id.登记册:
意图i=新意图(Login.this、Register.class);
星触觉(i);
打破
*/
违约:
打破
}
}
//AsyncTask是一个独立于运行GUI的线程的线程
//任何类型的网络都应该使用asynctask完成。
类AttemptLogin扩展了AsyncTask{
//调用三个方法,首先是preexecute,然后在后台执行,一次执行
//在后台完成后,将调用onPost Execute方法。
布尔失败=假;
字符串res;
字符串用户名;
字符串密码;
成功;
公共尝试登录(字符串用户名、字符串密码){
this.Username=用户名;
this.Password=密码;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
setMessage(“正在尝试登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
试一试{
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
Log.d(“请求!”,“启动”);
//通过发出HTTP请求获取产品详细信息
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
系统输出打印(“此处”);
//检查日志中的json响应
Log.d(“登录尝试”,json.toString());
//json成功标记
success=json.getInt(TAG_success);
res=json.getString(TAG_消息);
返回res;
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(int成功){
//删除产品后关闭对话框
pDialog.disclose();
如果(成功==1){
Log.d(“登录成功!”,res);
Intent i=新Intent(Login.this,main menu.class);
星触觉(i);
Toast.makeText(Login.this,res,Toast.LENGTH_LONG).show();
}否则{
Log.d(“登录失败!”,res);
Toast.makeText(Login.this,res,Toast.LENGTH_LONG).show();
}
}
}
}
正如logcat所说,我成功登录,但在我的android手机中仍然有
尝试登录…
,它不会进入我的主菜单界面。没有编译和运行时错误。我想知道Intent I=newintent(Login.this,main menu.class)代码>和起始触觉(i)代码>不起作用?您应该从doInBackground
返回success
,而不是res
。AsyncTask
中各种方法的类型(参数、返回)也有问题。doInBackground
的返回类型应与onPostExecute
的参数类型相同
按如下方式更改代码:
class AttemptLogin extends AsyncTask<String, String, Integer> {
...
protected Integer doInBackground(String... args) {
...
return success;
}
protected void onPostExecute(Integer success) {
...
}
}
class AttemptLogin扩展了AsyncTask{
...
受保护的整数doInBackground(字符串…args){
...
回归成功;
}
受保护的void onPostExecute(整数成功){
...
}
}
Log.d(“登录成功!”,res)
在日志中看到了吗?当onPostExecute没有执行时,我在日志中没有看到。您返回doInbackground中的字符串。因此,onPostExecute应该期望字符串作为参数。请查看asynctask文档以了解更多信息。