【发布时间】:2017-08-19 21:59:17
【问题描述】:
我目前正在尝试将 android 应用程序与服务器连接的教程(链接:https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/)。 但是,我对应该加载数据库元素的 AsyncTask 有困难。它会导致应用程序崩溃并且不会在 logcat 中留下错误日志(也尝试过“无过滤器”)。我也用 try catch 块包围了执行调用,但应用程序仍然崩溃。任何想法是什么导致了错误?
以下是代码摘录:
public class AllProductsActivity extends ListActivity {
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
...
@Override
public void onCreate(Bundle savedInstanceState) {
try {
new LoadAllProducts().execute();
}catch (Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
...
/**
* 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(AllProductsActivity.this);
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<Pair> params = new ArrayList<Pair>();
// 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
Intent i = new Intent(getApplicationContext(),
AddNewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} 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(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
【问题讨论】:
-
告诉我我可以在你的代码中看到的一件事 // 检查你的日志猫是否有 JSON 响应 Log.d("All Products: ", json.toString());请告诉我你可以在日志中看到 json.toString() 代码的内容吗?
标签: android android-studio android-asynctask