我有一个带导航抽屉和片段的Android应用程序。我试图用mySql(xampp)中的数据库填充listView。要从数据库中检索数据,我使用php脚本。
片段:
public class CommunityFragment extends ListFragment {
// 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 String url_all_products = "http://127.0.0.1/conectareBD/afiseaza_date_clienti_fizici.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CLIENTI = "clienti";
private static final String TAG_ID = "ID_Client";
private static final String TAG_NUME = "Nume";
private static final String TAG_PRENUME = "Prenume";
private static final String TAG_ADRESA="Adresa";
private static final String TAG_TELEFON="Telefon";
private static final String TAG_EMAIL="Email";
private static final String TAG_TIP_CLIENT="TipClient";
private static final String TAG_CREATE_AT="CreatAT";
// products JSONArray
JSONArray clienti = null;
EditText inputSearch;
ArrayAdapter<String> adapter;
public CommunityFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_community, container, false);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
//ListView lv = getListView();
ListView lv = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
/**
* 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
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
* */
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("Toti clientii: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
clienti = json.getJSONArray(TAG_CLIENTI);
int nr=clienti.length();
Log.d("lungime ",nr+" ");
// looping through All Products
for (int i = 0; i < clienti.length(); i++) {
JSONObject c = clienti.getJSONObject(i);
Log.d("obiect ",i+" " +c.toString());
// Storing each json item in variable
String id = String.valueOf(i); //c.getString(TAG_ID);
String nume = c.getString(TAG_NUME);
String prenume = c.getString(TAG_PRENUME);
String adresa=c.getString(TAG_ADRESA);
String email=c.getString(TAG_EMAIL);
String telefon=c.getString(TAG_TELEFON);
String createAT=c.getString(TAG_CREATE_AT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NUME, nume);
map.put(TAG_PRENUME, prenume);
map.put(TAG_ADRESA, adresa);
map.put(TAG_TELEFON, telefon);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
//open a new fragment
Fragment fragment = new PhotosFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_container, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null).commit();
}
} 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
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_ID,
TAG_NUME,TAG_PRENUME},
new int[] { R.id.pid, R.id.name,R.id.prenume });
// updating listview
setListAdapter(adapter);
}
});
}
}
我有以下错误:
04-20 17:06:09.555: W/System.err(689): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
04-20 17:06:09.635: W/System.err(689): at libcore.io.IoBridge.socket(IoBridge.java:568)
04-20 17:06:09.665: W/System.err(689): ... 21 more
04-20 17:06:09.665: E/Buffer Error(689): Error converting result java.lang.NullPointerException
04-20 17:06:09.684: E/JSON Parser(689): Error parsing data org.json.JSONException: End of input at character 0 of
04-20 17:06:09.814: W/dalvikvm(689): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
04-20 17:06:09.834: I/Choreographer(689): Skipped 39 frames! The application may be doing too much work on its main thread.
04-20 17:06:09.994: E/AndroidRuntime(689): FATAL EXCEPTION: AsyncTask #1
04-20 17:06:09.994: E/AndroidRuntime(689): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 17:06:09.994: E/AndroidRuntime(689): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 17:06:09.994: E/AndroidRuntime(689): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.lang.Thread.run(Thread.java:856)
04-20 17:06:09.994: E/AndroidRuntime(689): Caused by: java.lang.NullPointerException
04-20 17:06:09.994: E/AndroidRuntime(689): at info.androidhive.slidingmenu.CommunityFragment$LoadAllProducts.doInBackground(CommunityFragment.java:173)
04-20 17:06:09.994: E/AndroidRuntime(689): at info.androidhive.slidingmenu.CommunityFragment$LoadAllProducts.doInBackground(CommunityFragment.java:1)
04-20 17:06:09.994: E/AndroidRuntime(689): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 17:06:09.994: E/AndroidRuntime(689): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 17:06:09.994: E/AndroidRuntime(689): ... 5 more
有人可以帮助我吗?
答案 0 :(得分:0)
使用10.0.2.2
访问主机的localhost
,因为在模拟器上,127.0.0.1
是official documentation中定义的模拟器本身的环回地址:
String url_all_products = "http://10.0.2.2/conectareBD/afiseaza_date_clienti_fizici.php";