gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:58:32 25 4
gpt4 key购买 nike

我正在尝试列出“员工”表中的数据,然后检查列出的员工是否也在“状态”表中。现在我让 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com