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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 seata使用出现报错,其他服务找不到seata
    • ¥15 怎么实现输入一个要删除的数后删除后显示剩余数再输入再删除显示剩余数(语言-c语言)
    • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
    • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
    • ¥15 Google speech command 数据集获取
    • ¥15 vue3+element-plus页面崩溃
    • ¥15 像这种代码要怎么跑起来?
    • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
    • ¥15 pyqt5tools安装失败
    • ¥15 mmdetection