private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> eventsList;
// url to get all products list
private static String url_all_products = "http://xx.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CUSTOMER = "customer";
private static final String TAG_PID = "pid";
private static final String TAG_FIRSTNAME = "firstname";
private static final String TAG_LASTNAME = "lastname";
private static final String TAG_ADDRESS = "address";
private static final String TAG_TOTALCOST = "totalCost";
// products JSONArray
JSONArray events = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_transaction);
// Hashmap for ListView
eventsList = 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
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(CustomerTransaction.this);
pDialog.setMessage("Loading. 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("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
events = json.getJSONArray(TAG_CUSTOMER);
// looping through All Products
for (int i = 0; i < events.length(); i++) {
JSONObject c = events.getJSONObject(i);
// Storing each json item in variable
String pid = c.getString(TAG_PID);
String firstname = c.getString(TAG_FIRSTNAME);
String lastname = "Time :" +c.getString(TAG_LASTNAME);
String address = "Date :" +c.getString(TAG_ADDRESS);
String totalCost = "Venue :" +c.getString(TAG_TOTALCOST);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, pid);
map.put(TAG_FIRSTNAME, firstname);
map.put(TAG_LASTNAME, lastname);
map.put(TAG_ADDRESS, address);
map.put(TAG_TOTALCOST, totalCost);
// adding HashList to ArrayList
eventsList.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
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
CustomerTransaction.this, eventsList,
R.layout.list_item4, new String[] { TAG_PID,TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS, TAG_TOTALCOST},
new int[] { R.id.pid, R.id.firstname, R.id.lastname, R.id.address, R.id.totalCost });
// updating listview
setListAdapter(adapter);
}
});
}
}
logcat:
04-16 18:34:22.706: E/WindowManager(1019): Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
04-16 18:34:22.706: E/WindowManager(1019): android.view.WindowLeaked: Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
04-16 18:34:22.706: E/WindowManager(1019): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-16 18:34:22.706: E/WindowManager(1019): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
04-16 18:34:22.706: E/WindowManager(1019): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.Dialog.show(Dialog.java:281)
04-16 18:34:22.706: E/WindowManager(1019): at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)
04-16 18:34:22.706: E/WindowManager(1019): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-16 18:34:22.706: E/WindowManager(1019): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-16 18:34:22.706: E/WindowManager(1019): at com.spyraa.bookstore.CustomerTransaction.onCreate(CustomerTransaction.java:61)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.Activity.performCreate(Activity.java:5104)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-16 18:34:22.706: E/WindowManager(1019): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 18:34:22.706: E/WindowManager(1019): at android.os.Looper.loop(Looper.java:137)
04-16 18:34:22.706: E/WindowManager(1019): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-16 18:34:22.706: E/WindowManager(1019): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:34:22.706: E/WindowManager(1019): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 18:34:22.706: E/WindowManager(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 18:34:22.706: E/WindowManager(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 18:34:22.706: E/WindowManager(1019): at dalvik.system.NativeStart.main(Native Method)
非常感谢你。
答案 0 :(得分:0)
使用那样的请求
HttpPost httpost = new HttpPost(url_all_products);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("test1","test1" ));
nvps.add(new BasicNameValuePair("test2", "test2" ));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = getResponse(httpost);
答案 1 :(得分:0)
我看到几个问题。我认为首先是你的问题。将进度条初始化从onPreExecute()
移出到类声明
class LoadAllProducts extends AsyncTask<String, String, String> {
pDialog = new ProgressDialog(CustomerTransaction.this); // move to here so you can cancel in onPostExecute()
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
runOnUiThread
中不需要onPostExecute()
,因为此方法在UI
主题上运行。您正在使用onPostExecute()
中的参数,但似乎没有从doInBackground()
返回任何内容。可能会有更多问题,但我会首先尝试这些事情并看看你得到了什么
了解如何使用logcat非常重要。这里它告诉你一个窗口已经泄漏,我认为这是由你初始化progressbar
的地方造成的。通常,您会看到一行显示“由空指针异常引起的”或其他异常。找到此行时,引用项目的第一行
at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)
例如,会告诉您第84行是您的错误发生的地方。这通常是一个开始寻找的好地方。希望这有帮助