Java Android-显示来自MySQL的数据
我对Android非常陌生,目前我正在开发一个应用程序,用户可以输入一次身份证号码(作为登录),他可以使用该应用程序的其余功能 我目前正忙于显示来自MySQL服务器的数据。使用用户输入的ID(唯一且只有用户的标识),我可以显示用户的信息(通过Java Android-显示来自MySQL的数据,java,android,mysql,json,Java,Android,Mysql,Json,我对Android非常陌生,目前我正在开发一个应用程序,用户可以输入一次身份证号码(作为登录),他可以使用该应用程序的其余功能 我目前正忙于显示来自MySQL服务器的数据。使用用户输入的ID(唯一且只有用户的标识),我可以显示用户的信息(通过TextView或其他方式) 这是我目前的代码: public class MainActivity3Activity extends Activity { HttpPost httppost; StringBuffer buffer
TextView
或其他方式)
这是我目前的代码:
public class MainActivity3Activity extends Activity {
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
TextView tv;
TextView tv2;
String get;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
tv = (TextView)findViewById(R.id.tv);
tv2 = (TextView)findViewById(R.id.tv2);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://usamobileapp.pe.hu/webservice/student_info.php");
SharedPreferences preferences = getSharedPreferences("rfid", Context.MODE_PRIVATE);
if(preferences.contains("rfid")){
get = preferences.getString("rfid", null);
}
}
公共类MainActivity3Activity扩展活动{
HttpPost-HttpPost;
字符串缓冲区;
HttpResponse响应;
HttpClient-HttpClient;
列出nameValuePairs;
ProgressDialog=null;
文本视图电视;
文本视图tv2;
字符串获取;
私有网络视图;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u main\u activity3);
tv=(TextView)findviewbyd(R.id.tv);
tv2=(TextView)findViewById(R.id.tv2);
webView=(webView)findviewbyd(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(“http://usamobileapp.pe.hu/webservice/student_info.php");
SharedReferences首选项=GetSharedReferences(“rfid”,Context.MODE\u PRIVATE);
if(preferences.contains(“rfid”)){
get=preferences.getString(“rfid”,null);
}
}
那么我的问题是,从这里我该怎么做呢?我非常熟悉httpost
,但我想知道如何在登录时使用之前输入的ID显示用户信息?我听到了类似JSON解析的事情,但我不太确定如何使用它
如何显示与其输入的ID匹配的用户信息?如何使用文本视图显示
谢谢你的帮助
另外,请忽略那里的webview
。如果我的应用程序真的连接到我的php,我只将其作为示例使用。1)
2) 我建议,在你的客户端(android)上接收API元素太容易了
3) 显示您的数据!将有助于:)
还要吗
这可能看起来很难,但如果你学习几天,你就会学到它。你需要在与UI分离的线程上执行网络操作。
阅读关于休息的内容
在客户端,对于RESTAPI,我喜欢使用+
或者php,使用─ 要使用MySql实现登录/注册系统,您需要一个服务器端API,例如在PHP中操作数据库
您需要在服务器端执行类似操作:
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
以及查询数据库的功能:
public function getUserByEmailAndPassword($username, $password) {
$query = $this->dbh->prepare("SELECT * FROM users2 WHERE username = :username");
$query->bindParam(':username', $username);
$result = $query->execute();
// check for results
if ($query->rowCount() > 0) {
$result = $query->fetch(PDO::FETCH_ASSOC);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
android“调用”php脚本:
private static String login_tag = "login";
public void loginUser(String username, String password) throws ExecutionException, InterruptedException {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
jsonParser = new DbHandler(activity, this, params).execute();
}
从MySql服务器传输的数据必须是JSON编码的
在android设备上,您必须读取JSON响应并相应地采取行动
请查看此处以了解更多详细信息
太好了。更像是这样。
public DbHandler1(Activity activity, MyCallback dbIntf, List<NameValuePair> params) {
this.activity = activity;
intf = dbIntf;
this.params = params;
}
public JSONObject makeHttpRequest() {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(MainActivity.baseUrl);
//If database contains greek characters instantiate with UTF-8 Encoding
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (HttpHostConnectException e) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, R.string.connection_error, Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
try {
//If database contains greek characters instantiate with UTF-8 Encoding
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
@Override
protected JSONObject doInBackground(Void... params) {
jObj = makeHttpRequest();
return jObj;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
try {
intf.onRemoteCallComplete(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
{
"tag": "login",
"success": 1,
"error": 0,
}