Java tutorial
/* Copyright 2012 Mikhail Chabanov Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package mc.lib.network; import java.io.File; import mc.lib.interfaces.OnCompleteListener; import org.json.JSONObject; import org.w3c.dom.Document; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.AsyncTask; /** * Since Android 4.0 you can not perform Network requests in UI stream. * Use this helper to do networking asynchronously. * */ public class AsyncNetworkHelper { public static final String DEFAULT_ENCODING = "UTF8"; public static void hasConnection(final Context context, final OnCompleteListener<Boolean> listener) { new AsyncTask<Void, Void, Boolean>() { @Override protected Boolean doInBackground(Void... params) { return NetworkHelper.hasConnection(context); } @Override protected void onPostExecute(Boolean res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void saveToFile(final String url, final File file, final OnCompleteListener<File> listener) { new AsyncTask<Void, Void, File>() { @Override protected File doInBackground(Void... params) { NetworkHelper.saveToFile(url, file); return file; } @Override protected void onPostExecute(File res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void getAsText(final String url, final OnCompleteListener<String> listener) { getAsText(url, DEFAULT_ENCODING, listener); } public static void getAsText(final String url, final String encoding, final OnCompleteListener<String> listener) { new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { return NetworkHelper.getAsText(url, encoding); } @Override protected void onPostExecute(String res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void getBitmap(final String url, final OnCompleteListener<Bitmap> listener) { new AsyncTask<Void, Void, Bitmap>() { @Override protected Bitmap doInBackground(Void... params) { return NetworkHelper.getBitmap(url); } @Override protected void onPostExecute(Bitmap res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void getDrawable(final String url, final OnCompleteListener<Drawable> listener) { new AsyncTask<Void, Void, Drawable>() { @Override protected Drawable doInBackground(Void... params) { return NetworkHelper.getDrawable(url); } @Override protected void onPostExecute(Drawable res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void getXml(final String url, final OnCompleteListener<Document> listener) { new AsyncTask<Void, Void, Document>() { @Override protected Document doInBackground(Void... params) { return NetworkHelper.getXml(url); } @Override protected void onPostExecute(Document res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } public static void getJson(final String url, final OnCompleteListener<JSONObject> listener) { new AsyncTask<Void, Void, JSONObject>() { @Override protected JSONObject doInBackground(Void... params) { return NetworkHelper.getJson(url); } @Override protected void onPostExecute(JSONObject res) { super.onPostExecute(res); listener.complete(res); } }.execute((Void) null); } }