2012-10-04 14 views
5

पर एंड्रॉइड ऐप में mysql (XAMPP) को जोड़ने से मैं अपने अंतिम वर्ष प्रोजेक्ट के लिए एंड्रॉइड एप्लिकेशन विकसित करने की कोशिश कर रहा हूं और मैं उस बिंदु पर हूं जहां मुझे अपने एप्लिकेशन में डेटाबेस कनेक्ट करने की आवश्यकता है। मैंने पहले आंतरिक और बाहरी SQLite डेटाबेस से कनेक्ट किया है लेकिन मेरा पर्यवेक्षक कहता है कि मुझे XAMPP लोकलहोस्ट का उपयोग करना चाहिए, इसलिए यह बाद में उचित होगा क्योंकि यह वास्तविक जीवन की स्थिति के समान है जहां एक वेब सर्वर का उपयोग किया जाएगा। यहां मेरे डीबीहेल्पर क्लास के लिए कोड है, जिसे मैंने नेट पर पाया नमूना कोड से अनुकूलित किया है और कुछ चीजें बदल दी हैं, लेकिन मुझे पूरा यकीन है कि त्रुटियों का एक टन है। जब बटन को देखने के लिए बटन दबाया जाता है, जो इस वर्ग का नेतृत्व करेगा, तो यह केवल एक खाली स्क्रीन दिखाएगा और लंबे समय बाद दुर्घटनाग्रस्त हो जाएगा।एंड्रॉइड: एम्यूलेटर

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); 
       } 
      }); 

     } 


    } 
} 

तो मुझे सच में नहीं पता कि क्या गलत है क्योंकि मैं एंड्रॉइड में अच्छा नहीं हूं और जैसे ही मैं आगे बढ़ रहा हूं। कोई भी जो मेरी मदद कर सकता है?

+0

शेयर logcat संदेशों – ariefbayu

+0

acces करने हम 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

हाय ऑनूर ए, अगर मेरा स्थानीयहोस्ट http: // localhost: 8081/phpmyadmin /। तो मेरा 10.0.2.2 वही रहता है या 10.0.2.2:8081 जोड़ने की आवश्यकता है? – whywhy

संबंधित मुद्दे