连接消息显示在我的对话框中,但从未建立连接。我知道我的连接代码有效,所以这不是问题。
public class Blast_Login extends Activity {
public static boolean allowLogin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_blast_login);
}
public void loginClicked(View view){
Intent intent = new Intent(this,Blast_Main.class);
EditText user = (EditText)findViewById(R.id.username);
String username = user.toString();
EditText pass = (EditText)findViewById(R.id.password);
String password = pass.toString();
new blast_dbConnect(username,password,Blast_Login.this).execute("connecting");
if(allowLogin==true) startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blast__login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class blast_dbConnect extends AsyncTask<String,String,Boolean> {
String username;
String password;
Context c;
ProgressDialog pd;
public blast_dbConnect(String user,String pass,Context context){
username = user;
password = pass;
c = context;
}
@Override
protected void onPreExecute(){
pd = new ProgressDialog(c);
pd.setMessage("starting");
pd.show();
}
@Override
protected Boolean doInBackground(String...params) {
boolean login = true;
String input = params[0];
publishProgress(input);
try{
Connection conn;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://server/db","user","password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(String.format("select * from users where username= '%s'",username));
if(rs.next()==true){
String passCheck = rs.getString("password");
System.out.println(passCheck);
if(passCheck.equals(password)) login = true;
}
}
catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e){
e.printStackTrace();
}
return login;
}
@Override
protected void onProgressUpdate(String...params){
pd.setMessage(params[0]);
}
protected void onPostExecute(boolean login){
Blast_Login.allowLogin = login;
}
}
logcat的输出如下......
01-23 12:55:29.062: I/ActivityManager(1234): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.blast/.Blast_Login (has extras)} from uid 10008 on display 0
01-23 12:55:29.073: W/AudioTrack(1234): AUDIO_OUTPUT_FLAG_FAST denied by client
01-23 12:55:29.259: I/art(947): Background concurrent mark sweep GC freed 781(33KB) AllocSpace objects, 0(0B) LOS objects, 90% free, 111KB/1135KB, paused 17.159ms total 39.040ms
01-23 12:55:29.266: I/ActivityManager(1234): Start proc com.example.blast for activity com.example.blast/.Blast_Login: pid=2348 uid=10057 gids={50057, 9997, 3003} abi=x86
01-23 12:55:29.279: I/art(2348): Not late-enabling -Xcheck:jni (already on)
01-23 12:55:29.560: D/OpenGLRenderer(2348): Render dirty regions requested: true
01-23 12:55:29.567: D/(2348): HostConnection::get() New Host Connection established 0xae0dad50, tid 2348
01-23 12:55:29.634: D/Atlas(2348): Validating map...
01-23 12:55:29.710: D/(2348): HostConnection::get() New Host Connection established 0xa6c670c0, tid 2363
01-23 12:55:29.756: I/OpenGLRenderer(2348): Initialized EGL, version 1.4
01-23 12:55:29.818: D/OpenGLRenderer(2348): Enabling debug mode 0
01-23 12:55:29.855: W/EGL_emulation(2348): eglSurfaceAttrib not implemented
01-23 12:55:29.855: W/OpenGLRenderer(2348): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c47dc0, error=EGL_SUCCESS
01-23 12:55:30.452: I/ActivityManager(1234): Displayed com.example.blast/.Blast_Login: +1s240ms
01-23 12:55:42.244: W/AudioTrack(1234): AUDIO_OUTPUT_FLAG_FAST denied by client
01-23 12:55:42.326: D/InputEventConsistencyVerifier(2348): KeyEvent: ACTION_UP but key was not down.
01-23 12:55:42.326: D/InputEventConsistencyVerifier(2348): in android.widget.EditText{339870e1 VFED..CL .F...... 179,558-589,637 #7f090042 app:id/password}
01-23 12:55:42.326: D/InputEventConsistencyVerifier(2348): 0: sent at 1635316000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1635316, downTime=1635228, deviceId=0, source=0x101 }
01-23 12:55:50.159: W/AudioTrack(1234): AUDIO_OUTPUT_FLAG_FAST denied by client
01-23 12:55:50.256: I/art(2348): Background partial concurrent mark sweep GC freed 2687(135KB) AllocSpace objects, 0(0B) LOS objects, 48% free, 1075KB/2MB, paused 5.593ms total 58.608ms
01-23 12:55:50.329: W/EGL_emulation(2348): eglSurfaceAttrib not implemented
01-23 12:55:50.329: W/OpenGLRenderer(2348): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c47ac0, error=EGL_SUCCESS
答案 0 :(得分:0)
您正在AsyncTask
中执行操作,因此结果不同步。当任务真正完成时,将allowLogin == true
时想要执行的代码移动到onPostExecute()
。