Android Open Source - AndroidNetworkKit A N Task






From Project

Back to project page AndroidNetworkKit.

License

The source code is released under:

Apache License

If you think the Android project AndroidNetworkKit 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 com.mindblowninnovation.ank;
//from  w  w  w . j a  va  2s .c o  m

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.json.JSONException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;

public abstract class ANTask extends AsyncTask<String, Void, Object>
{
  private final String TAG = "ANTask";
  private Handler UIHandler;
  private String URL;
  private String action;
  private List<NameValuePair> params;
  private String queryString;
  private static final String baseURL = "http://ws.spotify.com/search/1/";
  private String password;
  private String username;
  private Context context;
  private String type;
  private static String userAgent;
  private boolean hasInternet;
  private boolean doBasicAuth;
  //unimplemented methods
  protected abstract Object decode(String jsondata) throws JSONException;
  
  public ANTask(String URL, String action, List<NameValuePair> params, String type, Handler mainUIHandler, Context context)
  {
    UIHandler = mainUIHandler;
    this.URL = URL;
    this.action = action; //POST - GET - POSTJSON
    this.params = params; //if post, use as post params, else turn into query string
    this.context = context;
    this.type = type;
    hasInternet=true;
    ANUtils util = new ANUtils(context);
    username = util.getUsername();
    password = util.getPassword();
    userAgent = ANUtils.getVersionString(context);
    doBasicAuth = (!username.equals("")) ? true:false;
    if(action.equals(ANUtils.ACTION_GET))
    {
      queryString = ANUtils.decodeParams(params, queryString);
    }
  }
  
  
  @Override
  protected Object doInBackground(String... arg0) 
  {
    if(ANUtils.isNetworkAvailable(context))
    {
      String data = "";
      //first get the data from the server
      if(action.equals(ANUtils.ACTION_POST)){
        data = doPost();
      }
      else if(action.equals(ANUtils.ACTION_GET)){
        data = doGet();
      }
      else{
        data = doPostJson();
      }
      if(data!= null)
      {
        Object toReturn=null;
        //now decode the response
        try {
          toReturn= decode(data);
        } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        return toReturn;
      }
      Log.d(TAG, "Error in doInBackground");
      return "ERROR";
    }
    Log.d(TAG, "Error: No internet");
    hasInternet = false;
    return null;
  }
  
  @Override
  protected void onPreExecute() 
  {
    super.onPreExecute();
    Log.d("URL", baseURL + URL);
  }
  
  @Override
  protected void onPostExecute(Object result) 
  {
    //Notify UI
    Message msg = Message.obtain();
    if(result != null){
      
          msg.what = 1; // 1 is succuess
          Bundle b = new Bundle();
          b.putString("type", type);
          msg.setData(b);
          msg.obj = result;
          
    }
    else if(!hasInternet){
      msg.what = 3;
    }
    else{
      msg.what = 2;//2= general error
    }
    UIHandler.sendMessage(msg);
        super.onPostExecute(result);
  }
  public void enableBasicAuth(Boolean shouldAuth){
    doBasicAuth = shouldAuth;
  }
  public String doPost()
  {
    Log.d("ASYCH", "Starting POST");
    StringBuilder builder = new StringBuilder();
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response;
    try 
        {
      
      String full_url = baseURL + URL;
      Log.d("URL", full_url);
      HttpPost httppost = new HttpPost(full_url);
      if(doBasicAuth){
        httppost.setHeader("Authorization", "Basic "+Base64.encodeToString(((username+":"+password).getBytes()), Base64.NO_WRAP));
      }
      
          httppost.setEntity(new UrlEncodedFormEntity(params));
          httppost.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
          response = client.execute(httppost);
          
          StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      Log.d("Status code", String.valueOf(statusCode));
      if (statusCode == 200) 
      {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null) 
        {
          builder.append(line);
        }
      } 
      else 
      {
        Log.e(ANTask.this.toString(), "Failed to download file");
        
      }
    } 
      catch (ClientProtocolException e) 
    {
      e.printStackTrace();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
    }
    finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            client.getConnectionManager().shutdown();
        }
    return builder.toString();
  
  }
  public String doGet()
  {
    Log.d("ASYCH", "Starting GET");
    StringBuilder builder = new StringBuilder();
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response;
    try 
        {
      
      String full_url = baseURL + URL + queryString;
      Log.d("URL", full_url);
      HttpGet httpget = new HttpGet(full_url);
      if(doBasicAuth){
        httpget.setHeader("Authorization", "Basic "+Base64.encodeToString(((username+":"+password).getBytes()), Base64.NO_WRAP));
      }
      httpget.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
          response = client.execute(httpget);
          HttpParams params = httpget.getParams();
          StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      Log.d("Status code", String.valueOf(statusCode));
      if (statusCode == 200) 
      {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null) 
        {
          builder.append(line);
        }
      } 
      else 
      {
        Log.e(ANTask.this.toString(), "Failed to download file");
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null) 
        {
          builder.append(line);
        }
        Log.d(TAG, "Error: " + builder.toString());
      }
    } 
      catch (ClientProtocolException e) 
    {
      e.printStackTrace();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
    }
    finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            client.getConnectionManager().shutdown();
        }
    return builder.toString();
  }
  public String doPostJson()
  {
    Log.d("ASYNC", "Starting POSTJSON");
    StringBuilder builder = new StringBuilder();
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response;
    try 
        {
      
      String full_url = baseURL + URL;
      Log.d("URL", full_url);
      HttpPost httppost = new HttpPost(full_url);
      if(doBasicAuth){
        httppost.setHeader("Authorization", "Basic "+Base64.encodeToString(((username+":"+password).getBytes()), Base64.NO_WRAP));
      }
      
      //passes the results to a string builder/entity
      BasicNameValuePair p = (BasicNameValuePair)params.get(0);
      String jsondata = p.getValue();
      Log.d("JSONDATAPARAM", jsondata);
        StringEntity se = new StringEntity(jsondata);
        
        //sets the post request as the resulting string
        httppost.setEntity(se);
        //sets a request header so the page receving the request
        //will know what to do with it
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");
        httppost.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);

        //Handles what is returned from the page 
          
          response = client.execute(httppost);
          
          StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      Log.d("Status code", String.valueOf(statusCode));
      if (statusCode == 200) 
      {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null) 
        {
          builder.append(line);
        }
      } 
      else 
      {
        Log.e(ANTask.this.toString(), "Failed to download file");
        
      }
    } 
      catch (ClientProtocolException e) 
    {
      e.printStackTrace();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
    }
    finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            client.getConnectionManager().shutdown();
        }
    return builder.toString();
  
  }
}




Java Source Code List

com.mindblowninnovation.ank.ANHandler.java
com.mindblowninnovation.ank.ANTask.java
com.mindblowninnovation.ank.ANUtils.java
com.mindblowninnovation.ank.Album.java
com.mindblowninnovation.ank.Artist.java
com.mindblowninnovation.ank.Image.java
com.mindblowninnovation.ank.MainActivity.java
com.mindblowninnovation.ank.Track.java