Android Open Source - android-gskbyte-utils Async U R L Request






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils 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 org.gskbyte.remote;
//from w w  w. j  a v  a2  s. c o m
import java.io.IOException;
import java.io.InputStream;

import org.gskbyte.util.IOUtils;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;

public class AsyncURLRequest
extends URLRequest
{

protected InputStream inputStream;

public interface CompletionListener
{
    public void onCompletion(AsyncURLRequest request, boolean success, IOException exception);
}

public interface RateListener
{
    public void onRate(AsyncURLRequest request, float rate);
}

public AsyncURLRequest(String urlBase)
{
    super(urlBase);
}

public AsyncURLRequest(String urlBase, String requestMethod)
{
    super(urlBase, requestMethod);
}

public InputStream getInputStream()
{ return this.inputStream; }

public String getString()
        throws IOException
{ return IOUtils.InputStreamToString(inputStream); }

public String getStringSafe()
{
    try {
        return IOUtils.InputStreamToString(inputStream);
    } catch (IOException e) {
        return null;
    }
}

@SuppressLint("NewApi")
public boolean executeAsync(CompletionListener listener, RateListener rateListener)
{
    if( this.isRunning() ) {
        return false;
    }
    
    DownloadTask task = new DownloadTask(listener, rateListener);
    
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else {
        task.execute();
    }
    
    return true;
}

private class DownloadTask
extends AsyncTask<Void, Float, Integer>
implements ProgressListener
{

protected final CompletionListener completionListener;
protected final RateListener rateListener;

public DownloadTask(CompletionListener listener, RateListener rateListener)
{
    this.completionListener = listener;
    this.rateListener = rateListener;
}

@Override
protected Integer doInBackground(Void... params)
{
    try {
        AsyncURLRequest.this.inputStream = AsyncURLRequest.this.execute(this);
    } catch (IOException e) {
        completionListener.onCompletion(AsyncURLRequest.this, false, e);
    }
    return AsyncURLRequest.this.getTotalBytes();
}

@Override
public void onProgressChanged(int readBytes, int totalBytes)
{
    if(rateListener != null)
        publishProgress(readBytes / (float)totalBytes);
}

@Override
protected void onProgressUpdate(Float ... values)
{
    float rate = values[0];
    rateListener.onRate(AsyncURLRequest.this, rate);
}

@Override
protected void onPostExecute(Integer bytes)
{
    completionListener.onCompletion(AsyncURLRequest.this, true, null);
}

}


}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java