2012-10-04 35 views
5

我想爲我的最後一年項目開發一個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,只是隨着我的繼續學習。任何人都可以幫助我?

+0

份額logcat的消息 – ariefbayu

+0

我們需要logcat的輸出能夠幫助你。 – Namphibian

+0

請分享您的故障記錄。 – VendettaDroid

回答

3

您應該使用10.0.2.2到進入您的本地主機,而不是10.0.0.2 :) http://developer.android.com/tools/devices/emulator.html#networkaddresses

+0

它已經過了幾年,因爲我發佈了這裏,但我想,我想!這也是當時的問題!感謝您的反應 –

+0

這沒什麼,它只是在未回答的問題上,所以我回復了:) –

+0

嗨Onur A,如果我的本地主機在http:// localhost:8081/phpmyadmin /。所以我的10.0.2.2應該保持不變或需要添加10.0.2.2:8081? – whywhy

相關問題