Android Utililty Methods URL Read

List of utility methods to do URL Read

Description

The list of methods to do URL Read are organized into topic(s).

Method

InputStreamgetResource(String filename)
get Resource
if (filename == null) {
    throw new IllegalArgumentException("Filename cannot be null");
URL url = r.getUC().getClass().getClassLoader()
        .getResource(filename);
if (url == null) {
    return null;
try {
    URLConnection connection = url.openConnection();
    connection.setUseCaches(false);
    return connection.getInputStream();
} catch (IOException ex) {
    r.log("Could not save " + filename);
return null;
StringreadFile(URL url)
read File
String result = "";
try {
    BufferedReader in = new BufferedReader(new InputStreamReader(
            url.openStream()));
    String inputLine = "";
    while (inputLine != null) {
        result += inputLine + "\n";
        inputLine = in.readLine();
...
StringgetXML(String url)
get XML
String responseBody = "";
try {
    HttpParams httpParameters = new BasicHttpParams();
    DefaultHttpClient httpClient = new DefaultHttpClient(
            httpParameters);
    HttpPost httpPost = new HttpPost(url);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
...
DocumentreadXmlFromUrl(String address)
read Xml From Url
Document document = null;
HttpURLConnection urlConnection = null;
try {
    URL url = new URL(address);
    urlConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = new BufferedInputStream(
            urlConnection.getInputStream());
    DocumentBuilderFactory factory = DocumentBuilderFactory
...
StringgetStringFromUrl(String url)
get String From Url
try {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    return EntityUtils.toString(httpEntity);
} catch (Exception e) {
    return null;
...
longgetFileDateTime(URL url)
Gets file date and time.
if (url == null) {
    return -1;
String fileName = url.getFile();
if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\') {
    fileName = fileName.substring(1, fileName.length());
try {
...
StringreadTextFromUri(Context c, Uri uri)
read Text From Uri
InputStream inputStream = c.getContentResolver().openInputStream(
        uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(
        inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
...
byte[]getHtmlByteArray(final String url)
get Html Byte Array
URL htmlUrl = null;
InputStream inStream = null;
try {
    htmlUrl = new URL(url);
    URLConnection connection = htmlUrl.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
...