java - 将 MySql 中两个表的数据导入 Android 应用程序

标签 java android mysql sql

我正在尝试列出“员工”表中的数据,然后检查列出的员工是否也在“状态”表中。现在我让 ListView 可以工作,但我似乎不知道如何连接到状态表以查看该员工是否使用他的 UserID 字段(在两个表中)存在。

我正在 Android 中执行以下操作:

连接:

package dbtesting.example.com.testexternaldb;

        import android.util.Log;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpResponse;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.impl.client.DefaultHttpClient;
        import org.apache.http.util.EntityUtils;
        import org.json.JSONArray;
        import org.json.JSONException;

        import java.io.IOException;

public class ApiConnector {


    public JSONArray GetAllCustomers()
    {
        // URL for getting all customers


        String url = "http://loguru.com/Android/getAllCustomers.php";

        // Get HttpResponse Object from url.
        // Get HttpEntity from Http Response Object

        HttpEntity httpEntity = null;

        try
        {

            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = LoginPage.httpclient.execute(httpGet);

            httpEntity = httpResponse.getEntity();



        } catch (ClientProtocolException e) {

            // Signals error in http protocol
            e.printStackTrace();

            //Log Errors Here



        } catch (IOException e) {
            e.printStackTrace();
        }


        // Convert HttpEntity into JSON Array
        JSONArray jsonArray = null;

        if (httpEntity != null) {
            try {
                String entityResponse = EntityUtils.toString(httpEntity);

                Log.e("Entity Response  : ", entityResponse);

                jsonArray = new JSONArray(entityResponse);

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return jsonArray;


    }


}

Android主要代码

public class GetAllCustomerListViewAdapter extends BaseAdapter {

    private JSONArray dataArray;
    private Activity activity;


    private static LayoutInflater inflater = null;

    public GetAllCustomerListViewAdapter(JSONArray jsonArray, Activity a) {

        this.dataArray = jsonArray;
        this.activity = a;

        inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return this.dataArray.length();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ListCell cell;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.get_all_customer_list_view_cell, null);
            cell = new ListCell();

            cell.FullName = (TextView) convertView.findViewById(R.id.customer_full_name);
            cell.Status = (TextView) convertView.findViewById(R.id.employee_status);

            cell.scan = (ImageView) convertView.findViewById(R.id.scan_status);

            convertView.setTag(cell);
        } else {
            cell = (ListCell) convertView.getTag();
        }

        try {
            JSONObject jsonObject = this.dataArray.getJSONObject(position);
            cell.Status.setText(jsonObject.getString("Status"));
            cell.FullName.setText(jsonObject.getString("Emp_F_Name") + " " + jsonObject.getString("Emp_L_Name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ListCell {
        private TextView FullName;
        private TextView Status;

        private ImageView scan;
    }
}

我的PHP代码如下:

  <?php

    $con = $con = mysql_connect("URL","user","pass");

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("project_nfc", $con);

    session_start();

    if (isset($_SESSION['group_id'])) {
        $result = mysql_query("SELECT * FROM Employee where Tag_ID!='".$_SESSION['tag_id']."' AND Group_ID='".$_SESSION['group_id']."'");

        }else{
        $result = mysql_query("SELECT * FROM Employee");
        }

    while($row = mysql_fetch_assoc($result))
      {
        $output[]=$row;
      }

    print(json_encode($output));

    mysql_close($con);


    ?>

我正在从 Employee 表中检索数据,但我还想从另一个名为 Status 的表中获取数据,我该怎么做?我需要另一个 PHP 文件吗?我可以在同一个 JSONArray 中执行此操作吗?

例如,我的代码现在从 Employee 检索状态、Emp_F_Name 和 Emp_L_Name,但我还需要从 EmpStatus 表检索状态。

最佳答案

您可以使用任意数据结构。 JSON 本身并不关心,它会编码几乎任何您想要生成的数据结构:

$data = array();
$data['status'] = ... data from status table query here
$data['employee'] = ... data from employee data here 

echo json_encode($data);

这只是意味着在 Java 端代码中需要进行更多处理才能使用这个"new"结构。

关于java - 将 MySql 中两个表的数据导入 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613655/

相关文章:

mysql - 如何从整数表中找到最接近指定整数的整数?

java - 节点部署期间 Corda 错误

Android kotlin MVVM - 你可以在 recyclerview 适配器中调用 viewmodel 吗?

android - react-native 错误找不到变量 : styles

android - 获取光标的结果并将其转换为 TextView 的字符串

php - MySQL查询从两个表中获取数据

java - 是否有任何基于浏览器的 IDE 来编写 java 程序?

java - Activity 结束后如何重置静态变量?

java - 如何使用jni将java中的Arraylist转换为c中的lpwstr

mysql - 为什么我的 MySQL 事务无法从 VB 运行?