Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created June 29, 2012 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspili/3018654 to your computer and use it in GitHub Desktop.
Save lukaspili/3018654 to your computer and use it in GitHub Desktop.
public class Application extends android.app.Application {
private static Context context;
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return Application.context;
}
}
public final class NetworkUtils {
public static boolean isOnline() {
NetworkInfo networkInfo = ((ConnectivityManager) Application.getContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (null == networkInfo || !networkInfo.isConnectedOrConnecting()) {
Log.d(NetworkUtils.class.getName(), "No network connection");
return false;
}
return true;
}
}
public final class UrlUtils {
private UrlUtils() {
}
public static URL getUrl(String urlAsString) {
URL url;
try {
url = new URL(urlAsString);
} catch (MalformedURLException e) {
Log.w(UrlUtils.class.getName(), "Invalid format for url : " + urlAsString, e);
return null;
}
return url;
}
public static String downloadData(String url) {
Log.d(UrlUtils.class.getName(), "Connection opened to : " + url);
long time = System.currentTimeMillis();
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(new HttpGet(url));
InputStream in = response.getEntity().getContent();
try {
return IOUtils.toString(in);
} finally {
in.close();
}
} catch (Exception e) {
Log.e(UrlUtils.class.getName(), "Error during downloading : " + e.getMessage());
return null;
} finally {
Log.d(UrlUtils.class.getName(), "Finish in " + (System.currentTimeMillis() - time) + " ms");
}
}
}
package com.siu.android.dondusang.util;
import android.util.Log;
import java.net.URLEncoder;
/**
* @author Lukasz Piliszczuk <lukasz.pili AT gmail.com>
*/
public class WebUtils {
public static final String UTF8 = "utf-8";
public static final String HTML = "text/html";
public static String encodeHtml(String content) {
try {
return URLEncoder.encode(content, UTF8).replaceAll("\\+", " ");
} catch (Exception e) {
Log.e(WebUtils.class.getName(), "Error encoding html in UTF8 : " + e.getMessage());
return content;
}
}
public static String wrapWithHtml(String content) {
return "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>" +
content +
"</body></html>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment