我目前正在开发一个Android项目,允许用户从数据库添加/更新/读取和删除记录。应用程序现在运行时没有任何错误,但是当我点击插入新记录或查看所有员工记录时,它只返回空白页面?我不认为它可以访问PHP文件或可能是其他任何东西?我有一个Config.java文件,其中我的php文件的地址上传到我的大学mudfoot服务器和一个ViewAllEmployee.java文件,当执行onClick时调用该文件应该使用JSON从我的数据库返回结果。任何人都可以指出我正确的方向如何纠正这一点,我一直在尝试最后几个小时,但似乎无法弄明白。
Config.java
package com.example.assignment.androidassignment;
public class Config {
//Address of our scripts of the CRUD
public static final String URL_ADD="http://www.mudfoot.doc.stu.mmu.ac.uk/students/abbasit/AndroidAssignment/addEmp.php";
public static final String URL_GET_ALL = "http://www.mudfoot.doc.stu.mmu.ac.uk/students/abbasit/AndroidAssignment//getAllEmp.php";
public static final String URL_GET_EMP = "http://mudfoot.doc.stu.mmu.ac.uk/students/abbasit/AndroidAssignment/getEmp.php?UserID=";
public static final String URL_UPDATE_EMP = "http://mudfoot.doc.stu.mmu.ac.uk/students/abbasit/AndroidAssignment/updateEmp.php";
public static final String URL_DELETE_EMP = "http://mudfoot.doc.stu.mmu.ac.uk/students/abbasit/AndroidAssignment/deleteEmp.php?UserID=";
//Keys that will be used to send the request to php scripts
public static final String KEY_EMP_ID = "UserID";
public static final String KEY_LAT = "Latitude";
public static final String KEY_LON = "Longitude";
public static final String KEY_TIMEINSERTED = "TimeInserted";
//JSON Tags
public static final String TAG_JSON_ARRAY="result";
public static final String TAG_ID = "UserID";
public static final String TAG_LAT = "Latitude";
public static final String TAG_LON = "Longitude";
public static final String TAG_TIMEINSERTED = "TimeInserted";
//employee id to pass with intent
public static final String EMP_ID = "UserID";
}
ViewAllEmployee.java
package com.example.assignment.assignment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ViewAllEmployee extends Activity implements ListView.OnItemClickListener {
private ListView listView;
private String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_employee);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getJSON();
}
private void showEmployee() {
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String UserID = jo.getString(Config.TAG_ID);
String Lat = jo.getString(Config.TAG_LAT);
HashMap<String, String> employees = new HashMap<>();
employees.put(Config.TAG_ID, UserID);
employees.put(Config.TAG_LAT, Lat);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
ViewAllEmployee.this, list, R.layout.list_item,
new String[]{Config.TAG_ID, Config.TAG_LAT},
new int[]{R.id.id, R.id.name});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(ViewAllEmployee.this, "Fetching Data", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(Config.URL_GET_ALL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, ViewEmployee.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String UserID = map.get(Config.TAG_ID).toString();
intent.putExtra(Config.EMP_ID, UserID);
startActivity(intent);
}
}
dbConnect.php
<?php
define('HOST','XXXX');
define('USER','XXXX');
define('PASS','XXXX');
define('DB','XXXX');
//Connecting to Database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
getAllEmp.php
<?php
//Importing Database Script
require_once('dbConnect.php');
//Creating sql query
$sql = "SELECT * FROM locationtrace";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"UserID"=>$row['UserID'],
"Latitude"=>$row['Latitude']
"Longitude"=>$row['Longitude']
"TimeInserted"=>$row['TimeInserted']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);