2012-10-04 34 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

相关问题