Android 我应该在我的alertdialog中添加到哪里,以及如何在json中获取值?
使用的教程链接: 问题是: 我的应用程序有一个有效的登录和注册活动。但是,我想在用户单击“登录/注册”按钮以显示一些信息后提示一个警报对话框 我按照上面的教程链接创建了AlertDialogManager.java。 这是我的登录按钮代码:Android 我应该在我的alertdialog中添加到哪里,以及如何在json中获取值?,android,android-alertdialog,Android,Android Alertdialog,使用的教程链接: 问题是: 我的应用程序有一个有效的登录和注册活动。但是,我想在用户单击“登录/注册”按钮以显示一些信息后提示一个警报对话框 我按照上面的教程链接创建了AlertDialogManager.java。 这是我的登录按钮代码: try{ int success = json.getInt(TAG_SUCCESS); //String mes
try{
int success = json.getInt(TAG_SUCCESS);
//String message = json.getString(TAG_MESSAGE);
//Log.v("SignUp", "Checkpoint 3");
if (success == 1) {
session.createLoginSession(email);
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity (i);
finish();
}else{
//failed to login
}
} catch (JSONException e){
e.printStackTrace();
}
当应用程序无法让用户登录时,我尝试在部分中添加alertdialog代码。我的alertdialog代码:
alert.showAlertDialog(Login.this,"Login failed", "Bla bla bla", false);
每当我尝试单击“登录”按钮时,整个应用程序都会崩溃,并显示以下日志:
05-15 10:08:55.641: W/dalvikvm(2338): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
05-15 10:08:55.701: E/AndroidRuntime(2338): FATAL EXCEPTION: AsyncTask #1
05-15 10:08:55.701: E/AndroidRuntime(2338): java.lang.RuntimeException: An error occured while executing doInBackground()
05-15 10:08:55.701: E/AndroidRuntime(2338): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-15 10:08:55.701: E/AndroidRuntime(2338): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
我对Android编程还是很陌生><有人能帮我吗?谢谢告诉我你们还需要什么,我会把它贴在这里
整个代码如下所示:
公共类登录扩展活动{
private ProgressDialog pDialog;
//Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
//Session manager
SessionManager session;
JSONParser jsonParser = new JSONParser();
EditText C_Email;
EditText C_Password;
public static final String DOMAIN = "192.168.0.112"; //Home
//public static final String DOMAIN = "172.18.74.146";
//URL to login
private static String url_login = "http://" + DOMAIN + "/iChop/login.php";
//JSON Node names
private static final String TAG_SUCCESS = "success";
//private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Session manager
session = new SessionManager(getApplicationContext());
//Edit Text
C_Email = (EditText) findViewById(R.id.C_Email);
C_Password = (EditText) findViewById(R.id.C_Password);
Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
//Login button
Button Login = (Button) findViewById(R.id.Login);
//Button click event
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new customer in background
new CustomerLogin().execute();
}
});
}
class CustomerLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Logging you in!");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args){
String email = C_Email.getText().toString();
String password = C_Password.getText().toString();
//Building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
/**
Log.d("C_Email", email);
Log.d("C_Password", password);
**/
//getting JSON object
JSONObject json = jsonParser.makeHttpRequest(url_login, "POST", params);
Log.v("SignUp", "Checkpoint 1");
//check log cat for response
Log.d("Create Response", json.toString());
Log.v("SignUp", "Checkpoint 2");
//check for success tag
try{
int success = json.getInt(TAG_SUCCESS);
//String message = json.getString(TAG_MESSAGE);
//Log.v("SignUp", "Checkpoint 3");
if (success == 1) {
session.createLoginSession(email);
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity (i);
finish();
}else{
//failed to login
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
}
}
@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;
}
public void SignUp (View view) {
Intent signup = new Intent (this, SignUp.class);
startActivity(signup);
}
}
private ProgressDialog pDialog;
//警报对话框管理器
AlertDialogManager alert=新建AlertDialogManager();
//会话管理器
会话管理器会话;
JSONParser JSONParser=新的JSONParser();
编辑文本C_电子邮件;
编辑文本C_密码;
公共静态最终字符串域=“192.168.0.112”;//主页
//公共静态最终字符串域=“172.18.74.146”;
//要登录的URL
私有静态字符串url\u login=“http://“+DOMAIN+”/iChop/login.php”;
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
//私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
//会话管理器
session=newsessionmanager(getApplicationContext());
//编辑文本
C_电子邮件=(EditText)findViewById(R.id.C_电子邮件);
C_密码=(EditText)findViewById(R.id.C_密码);
Toast.makeText(getApplicationContext(),“用户登录状态:+session.isLoggedIn(),Toast.LENGTH_LONG).show();
//登录按钮
按钮登录=(按钮)findViewById(R.id.Login);
//按钮点击事件
Login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//在后台创建新客户
新建CustomerLogin().execute();
}
});
}
类CustomerLogin扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
setMessage(“让您登录!”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
字符串email=C_email.getText().toString();
字符串密码=C_password.getText().toString();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“电子邮件”),电子邮件);
添加(新的BasicNameValuePair(“密码”,password));
/**
Log.d(“C_电子邮件”,电子邮件);
Log.d(“C_密码”,密码);
**/
//获取JSON对象
JSONObject json=jsonParser.makeHttpRequest(url_login,“POST”,params);
Log.v(“注册”、“检查点1”);
//检查cat日志以获取响应
d(“创建响应”,json.toString());
Log.v(“注册”、“检查点2”);
//检查成功标签
试试{
int success=json.getInt(TAG_success);
//String message=json.getString(TAG_message);
//Log.v(“注册”、“检查点3”);
如果(成功==1){
会话。创建登录会话(电子邮件);
Intent i=新Intent(getApplicationContext(),Home.class);
星触觉(i);
完成();
}否则{
//登录失败
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.login,menu);
返回true;
}
公共无效注册(视图){
意向注册=新意向(这个,signup.class);
startActivity(注册);
}
}
这看起来像是从非UI线程的线程创建此对话框,例如异步任务。必须从UI线程显示对话框。如果登录成功与否,让您的AsyncTask报告回启动它的活动,然后显示您的对话框:)
编辑:
AsyncTask当前返回一个字符串
,但该字符串始终为null
,因此显然不返回任何内容。允许您为“成功”返回布尔值(无论登录是否成功)。
将
CustomerLogin extends AsyncTask
更改为CustomerLogin extends AsyncTask
,并为登录成功返回一个布尔值(而不是您现在执行的null
字符串)。然后,您可以在onPostExecute
中使用这个bolean,它再次在UI线程上运行,您可以从那里创建您的对话框 这看起来像是从非UI线程的线程创建此对话框,例如AsyncTask。必须从UI线程显示对话框。如果登录成功与否,让您的AsyncTask报告回启动它的活动,然后显示您的对话框:)
编辑:
AsyncTask当前返回一个字符串
,但该字符串始终为null
,因此显然不返回任何内容。允许您为“成功”返回布尔值(无论登录是否成功)。
将CustomerLogin extends AsyncTask
更改为CustomerLogin extends AsyncTask
,并为登录成功返回一个布尔值(而不是执行此操作的null
字符串)
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the UI
@Override
public void run() {
// Update the UI, call your asynctask
}
});
}
};
new Thread(runnable).start();
}
if (success == 1) {
session.createLoginSession(email);
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity (i);
finish();
}else{
alert.showAlertDialog(Login.this,"Login failed", "Bla bla bla", false);
}