我创建登录页面。此登录页面从服务器数据库验证。如果网络可用意味着单击登录按钮表示它工作正常。如果我禁用网络然后单击登录按钮意味着它显示错误应用程序没有响应..如何使用我的代码避免此错误
i try this code
login.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
name = username.getText().toString();
pass = password.getText().toString();
String Str_check2 = app_preferences.getString("checked", "no");
if(Str_check2.equals("yes"))
{
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", name);
editor.putString("password", pass);
editor.commit();
}
if(name.equals("") || pass.equals(""))
{
Toast.makeText(Main.this, "Please Enter Username and Password", Toast.LENGTH_LONG).show();
}
else
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("url/login.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
catch (IOException e) {
System.out.println(e);
//alertDialog.cancel();
}
if(buffer.charAt(0)=='Y')
{
Intent intent=new Intent(getApplicationContext(),another.class);
startActivity(intent);
}
else
{
System.out.println("Invalid username and password")
}
}
});