我正在尝试为我的最后一年项目开发一个 android 应用程序,我正处于需要将数据库连接到我的应用程序的地步。我之前已经连接到内部和外部 SQLite 数据库,但我的主管说我必须使用 XAMPP localhost,所以稍后它是合理的,因为它与使用 Web 服务器的现实情况非常相似。这是我的 DBHelper 类的代码,我改编自我在网上找到的示例代码并确实改变了一些东西,但我很确定有很多错误。当按下会导致该类的查看数据的按钮时,它只会显示一个空白屏幕,并且在很长一段时间后会崩溃。
package com.example.parking_guide;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DBHelper extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static final String url_all_products = "http://10.0.0.2/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String KEY_ROWID = "_id";
private static final String KEY_VACANT = "vacancy";
// products JSONArray
JSONArray table = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView yourListView = getListView();
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("Level1: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
table = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < table.length(); i++) {
JSONObject c = table.getJSONObject(i);
// Storing each json item in variable
String _id = c.getString(KEY_ROWID);
String vacant = c.getString(KEY_VACANT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ROWID, _id);
map.put(KEY_VACANT, vacant);
// adding HashList to ArrayList
productsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DBHelper.this, productsList,
R.layout.list_item, new String[] { KEY_ROWID,
KEY_VACANT},
new int[] { R.id.id, R.id.vac});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
所以我真的不知道出了什么问题,因为我不太擅长 android,只是在继续学习。谁能帮助我?