Read from a URL : Url « Network « Android






Read from a URL

 
/* Copyright (C) 2009  Axel M?<axel.mueller@avanux.de> 
 * 
 * This file is part of LiveTracker.
 * 
 * LiveTracker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LiveTracker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LiveTracker.  If not, see <http://www.gnu.org/licenses/>.
 */
//package de.avanux.android.livetracker2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class HttpUtil {

  // lat=50.2911 lon=8.9842

  private final static String TAG = "LiveTracker:HttpUtil";

  public static String get(String url) throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP GET " + url);
    HttpGet method = new HttpGet(url);
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
  }

  public static String post(String url, Map<String, String> httpParameters) throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP POST " + url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(httpParameters.size());
    Set<String> httpParameterKeys = httpParameters.keySet();
    for (String httpParameterKey : httpParameterKeys) {
      nameValuePairs.add(new BasicNameValuePair(httpParameterKey, httpParameters.get(httpParameterKey)));
    }

    HttpPost method = new HttpPost(url);
    method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
  }

  private static HttpResponse executeMethod(HttpRequestBase method) throws ClientProtocolException, IOException {
    HttpResponse response = null;
    HttpClient client = new DefaultHttpClient();
    response = client.execute(method);
    Log.d(TAG, "executeMethod=" + response.getStatusLine());
    return response;
  }

  private static String getResponseAsString(HttpResponse response) throws IllegalStateException, IOException {
    String content = null;
    InputStream stream = null;
    try {
      if (response != null) {
        stream = response.getEntity().getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        BufferedReader buffer = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String cur;
        while ((cur = buffer.readLine()) != null) {
          sb.append(cur + "\n");
        }
        content = sb.toString();
      }
    } finally {
      if (stream != null) {
        stream.close();
      }
    }
    return content;
  }

}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the bit.ly service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Pull the raw text content of the given URL.
14.Get Video from URL
15.Gets data from URL
16.get Url Response
17.URL Encode Utils
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url