Android Open Source - PrincePolo Request Task






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo 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 se.agile.asynctasks;
//from w ww . java 2  s.c  om
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import se.agile.activities.MainActivity;
import se.agile.model.Preferences;
import android.os.AsyncTask;
import android.util.Log;


/**
 * So this abstract class it to make our http request a bit "easier" to implement.
 * 
 * @author Jacob
 *
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class RequestTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result>{
  private final String logTag = "PrincePolo";
  
  private RequestListener<Result> listener;
  
  private Result result;
  
  public RequestTask(){
    this(null);
  }
  
  public RequestTask(RequestListener<Result> listener){
    this.listener = listener;
  }
  
  @Override
  protected void onPreExecute (){
    if(!MainActivity.isNetworkConnected()){
      Log.e(logTag, "Warning: No Internet Connection");
      if(listener != null){
        listener.whenNoInternetConnection();
        abortRequest(false);
      }
    }
  }
  
  /**
   * General class for doing the GET http request to GitHub. It will automatically add the access_token.
   * What you have to do is to parse the json string that is returned from the http request.
   * 
   * 
   * @param url
   * @return the respond (as a json string) or null if the responding message's statuscode wasn't "OK"
   */
  protected String generalGETRequest(String url) {
    if(!isCancelled()){
      HttpClient client = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet(url);
      Log.d(logTag,"generalGETRequest: url -> " + url);
      try {
        httpGet.addHeader("Authorization", "token " + Preferences.getAccessToken());
        HttpResponse response = client.execute(httpGet);
        HttpEntity respEntity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
          BufferedReader reader = new BufferedReader(new InputStreamReader(respEntity.getContent(), "UTF-8"));
          StringBuilder builder = new StringBuilder();
          String line;
          while ((line = reader.readLine()) != null) {
            builder.append(line);
          }
          return builder.toString();
        }else{
          Log.e(logTag, "Didn't get statuscode 200");
        }
      } catch (ClientProtocolException e) {
        Log.e(logTag, "generalGETRequest: ClientProtocolException");
        e.printStackTrace();
      } catch (IOException e) {
        Log.e(logTag, "generalGETRequest: IOException");
        e.printStackTrace();
      }
    }
    return null;
  }
  
  public void finishedWithRequest(Result result){
    if(result == null){
      Log.e(logTag, "RequestTask: Result is null!");
      listener.requestFailed();
    }else{
      this.result = result;
      if(listener != null && !isCancelled()){
        listener.requestFinished(this.result);
      }
    }
  }
  
  public Result getResult(){
    if(result == null){
      Log.i(logTag, "RequestTask 'getResult' returned null");
    }
    return this.result;
  }
  
  /**
   * Remove some references. But otherwise it is just like calling cancel(but better).
   * You're recommended to set mayInterruptIfRunning = false; (if you don't want to, talk to Jacob....
   * 
   * @param mayInterruptIfRunning
   * @return
   */
  public boolean abortRequest(boolean mayInterruptIfRunning){
    //remove references so the garbagecollector can work....
    this.listener = null;
    this.result = null;
    return super.cancel(mayInterruptIfRunning);
  }
  
  public String getSelectedRepositoryName(){
    String repoName = Preferences.getSelectedRepository().getName();
    if(repoName.matches("[A-z0-9]+/[A-z0-9]+")){
      return repoName;
    }else{
      Log.e(logTag, "RequestBranch: Couldn't get selected repository");
      listener.whenNoSelectedRepository();
      abortRequest(false);
      return "";
      
    }
    
  }

}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java