gpt4 book ai didi

php - 在 Activity 的 "onCreate"方法中从在线 MySQL DB 获取数据

转载 作者:行者123 更新时间:2023-11-29 05:06:21 25 4
gpt4 key购买 nike

我正在开发需要从在线 MySQL 数据库获取数据并将其显示为 Activity 布局列表的应用程序。

问题是我的应用程序收到错误“尝试在空对象引用上调用接口(interface)方法‘int java.util.List.size()’”

在 onCreate 方法中,我调用 asyncTask 类以从服务器获取数据,然后在该行代码下方,我通过适配器将该数据设置为我的布局 ListView 。开始该 Activity 时,我收到错误消息。

我应该在启动 Activity 之前从服务器获取数据吗?还是问题出在其他地方?当我在浏览器中检查时,我的 php 文件工作正常。

这里是onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice_list);
final ListView listView1 = (ListView) findViewById(R.id.invoicelist);
InvoiceDownloader task = new InvoiceDownloader();

task.execute(new String[] { "http://10.0.2.2/company/getinvoices.php" });
ArrayAdapter<Invoice> adapter = new ArrayAdapter<Invoice>(this, android.R.layout.simple_list_item_1, invoices);
listView1.setAdapter(adapter);
}

编辑:

我按照大家的建议修复了它,现在没有错误, Activity 打开了,但它仍然没有显示来自在线数据库的任何数据。这是编辑的 onCreate() 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.invoice_list);
adapter = new ArrayAdapter<Invoice>(this, android.R.layout.simple_list_item_1);
listView1 = (ListView) findViewById(R.id.invoicelist);
InvoiceDownloader task = new InvoiceDownloader();
task.execute(new String[] { "http://10.0.2.2/company/getinvoices.php" });
adapter.notifyDataSetChanged();
}

这是 AsyncTask 类:

public class InvoiceDownloader extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
String response = "";
try {
response += getHttpContent(params[0]);
} catch (IOException e) {
Log.e("error", e.toString());
}
return response;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
adapter.addAll(invoices);
listView1.setAdapter(adapter);

Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}

public String getHttpContent(String urlStr) throws IOException {

String response = "";

try {
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}

JSONArray jsonArray = new JSONArray(sb.toString().trim());

for (int i = 0; i < jsonArray.length(); i++) {
Invoice invoice = new Invoice();
JSONObject inv = jsonArray.getJSONObject(i);

String companyNameOne = inv.getString("companyNameOne");
String companyNameTwo = inv.getString("companyNameTwo");
String address = inv.getString("address");
String postalCode = inv.getString("postalCode");
String city = inv.getString("city");
String oib = inv.getString("oib");
String email = inv.getString("email");
int comple = inv.getInt("completed");
String completed = String.valueOf(comple);
String workOrderNumber = inv.getString("workOrderNumber");
String price = inv.getString("price");

System.out.println("Company Name 1: " + companyNameOne + " Company Name 2: " + companyNameTwo + " address: " + address + " postalCode: " + postalCode + " city: " + city + " oib: " + oib + " email: " + email + " completed: " + completed + " work order num: " + workOrderNumber + " price: " + price);

invoice.setCompanyNameOne(companyNameOne);
invoice.setCompanyNameTwo(companyNameTwo);
invoice.setAddress(address);
invoice.setPostalCode(postalCode);
invoice.setCity(city);
invoice.setOib(oib);
invoice.setEmail(email);
invoice.setCompleted(completed);
invoice.setWorkOrderNumber(workOrderNumber);
invoice.setPrice(price);

invoices.add(invoice);
}
return response;
} catch (Exception e) {
return null;
}
}
}

第二次编辑:

我明白了。抱歉,这个问题在修复错误后不显示数据是我的错误。在这部分:

JSONObject inv = jsonArray.getJSONObject(i);

String companyNameOne = inv.getString("companyNameOne");
String companyNameTwo = inv.getString("companyNameTwo");
String address = inv.getString("address");
String postalCode = inv.getString("postalCode");
String city = inv.getString("city");
String oib = inv.getString("oib");
String email = inv.getString("email");
int comple = inv.getInt("completed");
String completed = String.valueOf(comple);
String workOrderNumber = inv.getString("workOrderNumber");
String price = inv.getString("price");

("") ALL 中的那些字符串必须是小写字母,因为我在我的 PHP 中是这样写的。

谢谢大家的帮助。

最佳答案

您应该通过适配器将数据设置为 asyncTask 的 onPostExecute() 上的 ListView 。 ( https://developer.android.com/reference/android/os/AsyncTask.html )

由于任务是异步的,您正试图在服务器响应到达之前访问发票列表的数据。

关于php - 在 Activity 的 "onCreate"方法中从在线 MySQL DB 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47295695/

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