Java 将MySql中两个表中的数据导入Android应用程序
我试图列出Employee表中的数据,然后检查列出的员工是否也在Status表中。现在我让listview工作了,但我似乎不知道如何连接到Status表,以查看该员工是否使用两个表中的UserID字段存在 我正在安卓系统中执行以下操作: 连接:Java 将MySql中两个表中的数据导入Android应用程序,java,android,mysql,sql,Java,Android,Mysql,Sql,我试图列出Employee表中的数据,然后检查列出的员工是否也在Status表中。现在我让listview工作了,但我似乎不知道如何连接到Status表,以查看该员工是否使用两个表中的UserID字段存在 我正在安卓系统中执行以下操作: 连接: package dbtesting.example.com.testexternaldb; import android.util.Log; import org.apache.http.HttpEntity;
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;
}
}
主安卓代码
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端代码中使用这种新结构需要更多的处理。我不太明白,在这个阶段它太令人困惑了:把json当作购物袋对待。你可以放面包,泡菜,水果等等。每一个都在自己的包装上贴上标签,塞进食品袋。你的android端代码打开包,然后取出水果并将其传递给任何想要的水果,打开腌菜,将其传递给代码的怀孕部分,诸如此类,但我的PHP端只带来一个sql语句中的数据,该语句连接到一个tableyes,这就是我的示例所显示的。运行多个代码位来获取所需的任何数据位,然后将每一位数据填充到最终json响应的自己部分中。我可以看一个示例吗?基本上,我想做的只是检查从employee表中提取的员工是否实际列在Status表中,因此不确定其是否正在检索数据。