我搜索了很多东西但没有用。所以这是我的问题。
我已成功使用responseHandler创建登录表单。这是代码:
public void login()
{
httpClient=new DefaultHttpClient();
httpPost= new HttpPost("http://studentstracking.hol.es/loginDatabase2.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",txtId.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("password",txtPassword.getText().toString().trim()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpClient.execute(httpPost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpClient.execute(httpPost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(LoginActivity.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(LoginActivity.this, student_activity.class));
}else{
showAlert();
}}
这是PHP代码:
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from user2 where kode_user2 = '".$username."' AND password = '".$password."'";
$query_exec = mysqli_query($conn, $query_search) or die(mysqli_error($conn));
$rows = mysqli_num_rows($query_exec);
if($rows == 0) {
echo "No Such User Found";
}
else {
echo "User Found";
}
但这次我想执行PHP并从中获取值并将其存储到变量中,或者将其存储到SQLite中。
这是PHP代码:
$query_search1 = "SELECT `schedule`.`kode_schedule`,`schedule`.`kode_matkul`,`matakuliah`.`title`,`schedule`.`ruang`,`kelas`.`latitude`,`kelas`.`longitude`,`kelas`.`lantai`,`schedule`.`hari`,`schedule`.`jam` FROM schedule LEFT JOIN `u466318196_track`.`matakuliah` ON `schedule`.`kode_matkul` = `matakuliah`.`kode_matkul` LEFT JOIN `u466318196_track`.`kelas` ON `schedule`.`ruang` = `kelas`.`ruang` WHERE(( kode_schedule = 'sch0001'))";
$query_exec1 = mysqli_query($conn, $query_search1) or die(mysqli_error($conn));
while ($data2 = mysqli_fetch_assoc($query_exec1))
{
$response['length'][] = $data2;
}
print json_encode($response);
这个json的结果是:
{
"length":
[
{
"kode_schedule":"sch0001",
"kode_matkul":"TIB01",
"title":"Basis Data",
"ruang":"501",
"latitude":"-6.18861653446272",
"longitude":"106.801122526546",
"lantai":"5",
"hari":"Selasa",
"jam":"17:45:00"
},
{
"kode_schedule":"sch0001",
"kode_matkul":"TIB02",
"title":"Pemrograman Berorientasi Objek",
"ruang":"LABB",
"latitude":"-6.18864706134991",
"longitude":"106.801161122636",
"lantai":"5",
"hari":"Selasa",
"jam":"19:30:00"
}
]
}
所以我想将“kode_schedule”,“kode_matkul”等值存储到变量中,例如String或者带有数组的字符串。
请帮助我。
答案 0 :(得分:0)
您将在响应字符串中获得结果。
JSONObject mainObject=new JSONObject(response);
for(int i: 0 and 1){
mainObject.getJSONArray("length").getJSONObject(i).getString("kode_schedule");
mainObject.getJSONArray("length").getJSONObject(i).getString("kode_matkul");
}
使用json方法进行相应的访问。他们有很多教程来学习json解析。