-1

I used this code to connect to MySQL using WAMP.
But it doesn't work

package com.first.ismael.material_drawer;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import  org.json.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
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;


 import android.widget.Toast;

/**
 * Created by ismae_000 on 11/20/2015.
 */
public class all_meals extends Fragment {

   public ProgressDialog pDialog ;

// Creating JSON Parser object
    JSONParser jParser = new JSONParser();


// url to get all products list
   private static String url_all_products =               "http://192.168.1.7/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "component";
private static final String TAG_PID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_TYPE ="type";
ListView lv;
// products JSONArray
JSONArray products = null;

ArrayList<HashMap<String, String>> productsList;
// url to get all products list


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.all,
            container, false);

    productsList = new ArrayList<HashMap<String, String>>();
    lv =  (ListView)view.findViewById(R.id.listView2);
    // Loading products in Background Thread
   new LoadAllProducts().execute();

    // Get listview

    return view;

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}


class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    @Override
     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("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);


                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
               Toast.makeText(getActivity(),"There is nothing",Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {


      pDialog.dismiss();
        // updating UI from Background Thread
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), productsList,
                        R.layout.list_item, new String[]{TAG_PID,
                        TAG_NAME},
                        new int[]{R.id.pid, R.id.name});
                // updating listview
                lv.setAdapter(adapter);
            }
        });
    }
}

    }

I used it in a Fragment.
Please help

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35

1 Answers1

0

If you are using WAMP means using localhost for database MySql. Ip address for localhost should 10.0.2.2 not 192.168.1.7

try to replace _http://192.168.1.7/get_all_products.php with _http://10.0.2.2/get_all_products.php

and check your output get_all_products.php, that's should be json format

read this - why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client

check json format use this - _http://jsonlint.com

Community
  • 1
  • 1
Irvan Dwi Pangga
  • 322
  • 4
  • 15