我正在做一个名为电话簿的安卓应用程序。教师联系人保存在本地主机的数据库中。只有当用户提供有效密码时,联系人才必须显示。我已经为登录界面编写了程序。如果密码正确,则从用户那里获取输入(密码),它必须显示“登录成功”,否则显示“失败”。我的代码中出现运行时错误。请帮帮我。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsn=(EditText)findViewById(R.id.edit);
login=(Button)findViewById(R.id.button1);
login.setOnClickListener(this);
}
public void onClick(View v)
{
httpclient = new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/log.php");
Usn=etUsn.getText().toString();
try
{
namevaluepair = new ArrayList<NameValuePair>(1);
namevaluepair.add(new BasicNameValuePair("Usn",Usn));
httppost.setEntity(new UrlEncodedFormEntity(namevaluepair));
response=httpclient.execute(httppost);
Toast.makeText(getBaseContext(), "hello", Toast.LENGTH_SHORT).show();
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
if(entity!=null)
{
InputStream instream=entity.getContent();
JSONObject jsonResponse=new JSONObject(convertStreamToString(instream));
String retUsn;
retUsn =jsonResponse.getString("usn");//table field
if(Usn.equals(retUsn))
{
SharedPreferences sp=getSharedPreferences("login details", 0);
SharedPreferences.Editor spedit= sp.edit();
spedit.commit();
Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Invalid usn", Toast.LENGTH_SHORT).show();
}
}
}
}catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(), "connection error", Toast.LENGTH_SHORT).show();
}
}