dongxuanjiao0795 2015-12-09 21:30
浏览 37

android没有从mysql数据库返回任何数据

i am currently working on a Android project that allows the user to Add/Update/Read and Delete records from a database. The application runs without any errors now but when i click on inserting a new record or viewing all employee records it returns nothing but a blank page? I don't think it can access the PHP files or could be anything else? I have a Config.java file with the addresses of my php files uploaded to my university mudfoot server and a ViewAllEmployee.java file that is called when the onClick is executed should return the results from my database using JSON. Can anyone point me in the right direction on how to rectify this, i have been trying for the last few hours but cant seem to figure it out.

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);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况