com.pansapiens.occyd.UrlFetch.java Source code

Java tutorial

Introduction

Here is the source code for com.pansapiens.occyd.UrlFetch.java

Source

//    This file is part of the Occyd Android Client 
//    - an Android application for geolocation tagging
//    Copyright (C) 2008, Andrew Perry (ajperry@pansapiens.com)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero General Public License as
//    published by the Free Software Foundation, either version 3 of the
//    License, or (at your option) any later version.
//
//    This program 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 Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.pansapiens.occyd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.os.Handler;
import android.os.Message;

public class UrlFetch {
    public String USER_AGENT = "OccydAndroid/1.0";
    private final Handler handler;
    private URL url;

    public UrlFetch(URL uri, Handler result_handler) {
        super();
        url = uri;
        handler = result_handler;
    }

    public void fetch() {
        String response = "";
        Message msg = Message.obtain();
        msg.what = 0;
        msg.obj = "empty";
        try {

            // create an http client with a get request
            // for our url
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(url.toString());

            // set the User-Agent
            List<Header> headers = new ArrayList<Header>();
            headers.add(new BasicHeader("User-Agent", USER_AGENT));
            httpClient.getParams().setParameter(ClientPNames.DEFAULT_HEADERS, headers);

            // execute the request
            HttpResponse resp = httpClient.execute(httpGet, localContext);

            BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
            String inputLine;

            response = "";
            while ((inputLine = in.readLine()) != null)
                response += inputLine;
            in.close();

            msg.what = 1;
            msg.obj = response;

        } catch (IOException e) {
            msg.what = 0;
            msg.obj = "io exception : " + url.toString();
        } finally {
            handler.sendMessage(msg);
        }
    }
}