我是android新手,请任何人都可以在这里帮助我,如何从只有注册用户才能登录的网站数据库中验证用户名和密码。如何从我的网站获取特定内容并将其显示在 android 应用程序中。
问问题
2675 次
3 回答
2
在应用程序中查看有两种类型的验证
1. Client side validation and 2. Server side validation
在这里,您询问Android App的服务器端登录身份验证。请参考此链接,这是您找到的很好的解决方案。在本教程中准确描述了您搜索的内容。
于 2012-11-01T10:53:28.470 回答
0
您在下面使用了 DbAdpter Class.my 代码:
public class DBAdpter {
public static String url = YourServerlink;
public static String loginInUser(String username, String password) {
String result = "";
String msg = "";
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url + "login.php?");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString().trim();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONObject jObj = new JSONObject(result);
msg = jObj.getString("Message");
} catch (JSONException e) {
e.printStackTrace();
}
return msg;
}
您使用登录页面将我的代码放在下面的创建方法上:
uname = (EditText) findViewById(R.id.user_edt_lg);
pass = (EditText) findViewById(R.id.pass_edt_lg);
login = (Button) findViewById(R.id.login);
reg_txt = (TextView) findViewById(R.id.reg_txt);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
strUname = uname.getText().toString().trim();
strPass = pass.getText().toString().trim();
DBAdpter.loginInUser(strUname, strPass);
Toast.makeText(QuizActivity.this, "Successfully login", 1)
.show();
Intent i = new Intent(QuizActivity.this, All_Quiz_list.class);
startActivity(i);
}
});
于 2012-11-01T09:49:47.000 回答
0
这是用户从 Web 服务器登录的好例子。
于 2012-11-01T09:50:33.467 回答