各位程序员,我在 Eclipse 中编写了一个 java 代码,它可以使用 localhost phpmyadmin 数据库登录到一个帐户,checklogin 正在工作,如果该帐户已注册,它让我登录,但如果我输入了错误的帐户,android 模拟器告诉我应用程序有崩溃了,我想有人帮助我.. 如果输入的信息有误,我想做的就是显示一条消息,告诉您用户名或密码错误希望有人可以帮助我
这是我的代码:
public class user extends Activity implements OnClickListener {
EditText etUser , etPass;
Button bLogin;
String username, password ;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs ;
HttpResponse response;
HttpEntity entity;
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user);
initialise();
}
private void initialise() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.editText2);
etPass = (EditText) findViewById(R.id.editText1);
bLogin = (Button) findViewById(R.id.button1);
bLogin.setOnClickListener(this);
}
public void onregister(final View button) {
final Intent intent = new Intent();
intent.setClass(this, register.class);
startActivity(intent);
}
public void onClick(final View v) {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/android/database.php");
username = etUser.getText().toString();
password = etPass.getText().toString();
try {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
final Thread a = new Thread(new Runnable() {
public void run() {
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (response.getStatusLine().getStatusCode() == 200) {
entity = response.getEntity();
if (entity != null) {
InputStream instream = null;
try {
instream = entity.getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonResponse = null;
try {
jsonResponse = new JSONObject(convertStreamToString(instream));
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String retUser = null;
try {
retUser = jsonResponse.getString("username");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String retPass = null;
try {
retPass = jsonResponse.getString("password");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (username.equals(retUser) && password.equals(retPass)) {
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("username", username);
spedit.putString("password", password);
spedit.commit();
Toast.makeText(getBaseContext(), "Succes!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
});
a.start();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
private static String convertStreamToString(final InputStream is) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (final IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}}