Android Open Source - android-ocw Get Category List Task






From Project

Back to project page android-ocw.

License

The source code is released under:

GNU General Public License

If you think the Android project android-ocw listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package app.ocw.task;
/*from w w w  . j ava 2s .c o  m*/
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import app.ocw.api.CategoryListAPI;
import app.ocw.model.Category;

/**
 * An async task for getting a list of categories from the OCW data API.
 * It will only use the first string in the ellipsis array. The background
 * thread will return NULL for a failed query.
 * 
 * @author Nick Ferraro
 *
 */
public abstract class GetCategoryListTask extends AsyncTask<String, Integer, List<Category>> implements CategoryListAPI {
  @Override
  protected final List<Category> doInBackground(String... params) {
    List<Category> categories = new ArrayList<Category>();
    
    if(params != null && params.length > 0) {
      String url = params[0];
      
      try {
        // Create our client, our request, and get our response
        HttpClient client = new DefaultHttpClient();
        HttpGet getCategoryList = new HttpGet(url);
        HttpResponse categoryListResponse = client.execute(getCategoryList);
        
        // Parse our response into a list of Category objects
        JSONArray categoryListArray = new JSONArray(EntityUtils.toString(categoryListResponse.getEntity()));
        for( int i = 0; i < categoryListArray.length(); ++i ) {
          JSONObject categoryObject = categoryListArray.getJSONObject(i);
          Category category = parseCategory(categoryObject, null);
          categories.add(category);
        }
      } catch(Exception e) {
        Log.e("GetCategoryListTask", e.getMessage());
        return null;
      }
    }
    
    return categories;
  }
  
  private Category parseCategory(JSONObject categoryObject, Category parent) throws JSONException {
    Category parsedCategory = new Category(categoryObject.getString(KEY_NAME), categoryObject.getInt(KEY_COURSE_COUNT), parent);
    
    List<Category> childrenList = parsedCategory.getChildren();
    JSONArray childrenArray = categoryObject.getJSONArray(KEY_CHILDREN);
    for( int i = 0; i < childrenArray.length(); ++i ) {
      childrenList.add(parseCategory(childrenArray.getJSONObject(i), parsedCategory));
    }
    
    return parsedCategory;
  }
}




Java Source Code List

app.ocw.CourseAdapter.java
app.ocw.CourseDetailActivity.java
app.ocw.CourseListActivity.java
app.ocw.CourseSearchActivity.java
app.ocw.api.CategoryListAPI.java
app.ocw.api.CourseSearchAPI.java
app.ocw.api.ProviderListAPI.java
app.ocw.model.Category.java
app.ocw.model.CourseDetail.java
app.ocw.model.Course.java
app.ocw.model.Provider.java
app.ocw.task.GetCategoryListTask.java
app.ocw.task.GetProviderListTask.java
app.ocw.task.SearchCoursesTask.java