samedi 28 février 2015

Android: getting data from database causes app to stop responding, Stuck on progress dialog



Hey guys im creating an app where it populates a list view with data from mysql, the data that will fill the list view consists of courseid, courseName and lecturerName. However when i click the button to view the list it creates the progress dialog as it should however it gets stuck and then the application stop responding.


Below is the code to which i believe is causing the error because the logcat mentions something about doInBackground which is in this class:


the log cat file is: http://ift.tt/1E5vjiM


i really appreciate your time and help, i further want to say i am sorry about my debugging skills im still getting used to android.



public class AllCoursesActivity extends ListActivity {

//progress dialog
private ProgressDialog pDialog;

//create json parser object to understand the php files that were created
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> courseList;

//url to get all the product list
private static String url_all_courses = "http://ift.tt/1AVISNV";


//JSON node Names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COURSES = "courses";
private static final String TAG_COURSEID = "courseid";
private static final String TAG_COURSENAME = "courseName";

//products JSON array
JSONArray courses =null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allcourses);

//hashmap for listview
courseList = new ArrayList<HashMap<String, String>>();

//loading courses in background thread
new LoadAllCourses().execute();

//GET list view
ListView lv = getListView();


}

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

//before starting the background thread show some progress dialog

protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(AllCoursesActivity.this);
pDialog.setMessage("Loading Courses. Please Wait");
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.show();
}

//getting all products from the URL
@Override
protected String doInBackground(String... args) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Getting JSON String from URL
JSONObject json = jsonParser.makeHttpRequest(url_all_courses, "GET", params);
//check log cat for json response
Log.d("All Products: ", json.toString());

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

if (success == 1){
//it means courses were found
//Getting Array of products
courses = json.getJSONArray(TAG_COURSES);

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

//storing each JSON Item in the variable
String courseid = c.getString(TAG_COURSEID);
String coursename = c.getString(TAG_COURSENAME);

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

//adding each child node to hashmap key => value
map.put(TAG_COURSEID, courseid);
map.put(TAG_COURSENAME, coursename);

//adding Hash list to array list
courseList.add(map);
}
}else {
//no courses found
//go back to dashboard
Intent i = new Intent(getApplicationContext(),MainScreenActivity.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 the courses
pDialog.dismiss();
//updating ui from background thread
runOnUiThread(new Runnable() {
@Override
public void run() {
//updating parsed JSon data into list view
ListAdapter adapter = new SimpleAdapter(AllCoursesActivity.this, courseList,
R.layout.listcourse, new String[]{TAG_COURSEID, TAG_COURSENAME},
new int[]{R.id.courseid, R.id.coursename});
//updating listview
setListAdapter(adapter);
}
});
}
}


}




Aucun commentaire:

Enregistrer un commentaire