我想爲我的最後一年項目開發一個Android應用程序,並且我需要將數據庫連接到我的應用程序。我以前連接到內部和外部的SQLite數據庫,但是我的主管說我必須使用XAMPP本地主機,所以稍後會有道理,因爲它非常類似於使用Web服務器的真實情況。 這裏是我的DBHelper類的代碼,我從我在網上找到的示例代碼改編而來,確實改變了一些東西,但我確信存在大量錯誤。 當按鈕來查看數據,這將導致這個類被按下,它只會顯示一個空白的屏幕,並在很長一段時間後會崩潰。Android:將mysql(XAMPP)連接到模擬器上的android應用程序
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,只是隨着我的繼續學習。任何人都可以幫助我?
份額logcat的消息 – ariefbayu
我們需要logcat的輸出能夠幫助你。 – Namphibian
請分享您的故障記錄。 – VendettaDroid