数据库中的数据未显示

时间:2013-02-12 05:57:02

标签: php android mysql ajax

您好我开发了一个Android应用程序,它从mysql数据库中检索数据,但数据没有显示。这是我的代码。

public class AllStoresFragment extends ListFragment{
    private String mClient;
    private String mBaseUrl="http://192.168.1.5/Flutura/PHP/";
    private String mDataUrl=mBaseUrl+"Core/Data/android.data.php";
    //private String mAssetsUrl=mBaseUrl+"Assets/";
    private ArrayList<String> mStoreDetailsList;
    private String mRequest="outlets";
    private String mLocation="all";
    private SharedPreferences myPrefs ;
    private StoreListItemAdapter mListAdapter;  

    public AllStoresFragment(StoreListItemAdapter listAdapter){
        mListAdapter = listAdapter;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myPrefs = getActivity().getSharedPreferences("myPrefs", Context.MODE_WORLD_READABLE);
        executeAjaxRequest();

        return super.onCreateView(inflater, container, savedInstanceState);
    } 

    @Override
    public void onStart() {
        super.onStart(); 
        /** Setting the multiselect choice mode for the listview */
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    private void executeAjaxRequest(){
        if(mListAdapter != null){
            mListAdapter.clear();
        }
        mClient = myPrefs.getString("client", "all");
        String url = mDataUrl+"?client="+mClient+"&request="+mRequest+"&location="+mLocation;       
        Log.v("url",url); 

        AsyncHttpClient httpclient = new AsyncHttpClient();
        httpclient.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                setOutletData(response);
            }
        });
    }

    private void setOutletData(String response){        
        /** Creating array adapter to set data in ListView using AJAX Data*/

        try{            
          ArrayList<StoreDetails> store_details_list = new ArrayList<StoreDetails>();
          Log.v("response",response);
          mStoreDetailsList = Utils.ToArrayList(new JSONArray(response));
          Log.v("store_list",mStoreDetailsList.toString());

          Iterator<String> i = mStoreDetailsList.iterator();          
          String json_store = "";

          //Loop the list
          while( i.hasNext() ){
            json_store = i.next();
            JSONObject store = new JSONObject( json_store );      

            //Create a StoreDetails object and add it to the ArrayList Of StoreDetails
            store_details_list.add( new StoreDetails( store ) );            
          }     
          //Set the data adapter for the List

          StoreListItemAdapter sla = new StoreListItemAdapter( getActivity() , R.layout.storelist_item , store_details_list );
          sla.notifyDataSetChanged();
          setListAdapter(sla  );
        }
        catch(JSONException e){
          e.printStackTrace();
        }

    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {  
      // TODO Auto-generated method stub      
      Intent myIntent = new Intent(getActivity(),StoreActivity.class);// getActivity needs to be called since this is a Fragment Class.
      SharedPreferences myPrefs = getActivity().getApplicationContext().getSharedPreferences("myPrefs",Context.MODE_WORLD_READABLE);
      SharedPreferences.Editor prefsEditor = myPrefs.edit();

      prefsEditor.putString("outlet_details", mStoreDetailsList.get(position));
      prefsEditor.commit();
      startActivity(myIntent);
   }
}

这是我的PHP代码。

<?php

//connect to the db
$user = "root";
$pswd = "";
$db = "recommendations_db";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db);
$query = "SELECT * FROM outlets";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

while($row = mysql_fetch_array($result))
  {
  $output[] = $row; 
  }
echo json_encode($output);
?>

1 个答案:

答案 0 :(得分:0)

试试这个。

while($row = mysql_fetch_assoc($result))
{
   $output[] = $row;
}

echo "<pre>"; // this is just test
print_r($output); // this is just test so after test remove it.

echo json_encode($output);

mysql_fetch_assoc()是具有键名的数组,因此它们更具人类可读性。