Android Open Source - android-augment-reality-framework Twitter Data Source






From Project

Back to project page android-augment-reality-framework.

License

The source code is released under:

GNU General Public License

If you think the Android project android-augment-reality-framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jwetherell.augmented_reality.data;
//  w  ww  .j av a 2  s .  c o m
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.jwetherell.augmented_reality.R;
import com.jwetherell.augmented_reality.ui.Marker;
import com.jwetherell.augmented_reality.ui.IconMarker;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

/**
 * This class extends DataSource to fetch data from Twitter.
 * 
 * @author Justin Wetherell <phishman3579@gmail.com>
 */
public class TwitterDataSource extends NetworkDataSource {

    private static final String URL = "http://search.twitter.com/search.json";

    private static Bitmap icon = null;

    public TwitterDataSource(Resources res) {
        if (res == null) throw new NullPointerException();

        createIcon(res);
    }

    protected void createIcon(Resources res) {
        if (res == null) throw new NullPointerException();

        icon = BitmapFactory.decodeResource(res, R.drawable.twitter);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String createRequestURL(double lat, double lon, double alt, float radius, String locale) {
        return URL + "?geocode=" + lat + "%2C" + lon + "%2C" + Math.max(radius, 1.0) + "km";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Marker> parse(String url) {
        if (url == null) throw new NullPointerException();

        InputStream stream = null;
        stream = getHttpGETInputStream(url);
        if (stream == null) throw new NullPointerException();

        String string = null;
        string = getHttpInputString(stream);
        if (string == null) throw new NullPointerException();

        JSONObject json = null;
        try {
            json = new JSONObject(string);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (json == null) throw new NullPointerException();

        return parse(json);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Marker> parse(JSONObject root) {
        if (root == null) throw new NullPointerException();

        JSONObject jo = null;
        JSONArray dataArray = null;
        List<Marker> markers = new ArrayList<Marker>();

        try {
            if (root.has("results")) dataArray = root.getJSONArray("results");
            if (dataArray == null) return markers;
            int top = Math.min(MAX, dataArray.length());
            for (int i = 0; i < top; i++) {
                jo = dataArray.getJSONObject(i);
                Marker ma = processJSONObject(jo);
                if (ma != null) markers.add(ma);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return markers;
    }

    private Marker processJSONObject(JSONObject jo) {
        if (jo == null) throw new NullPointerException();

        if (!jo.has("geo")) throw new NullPointerException();

        Marker ma = null;
        try {
            Double lat = null, lon = null;

            if (!jo.isNull("geo")) {
                JSONObject geo = jo.getJSONObject("geo");
                JSONArray coordinates = geo.getJSONArray("coordinates");
                lat = Double.parseDouble(coordinates.getString(0));
                lon = Double.parseDouble(coordinates.getString(1));
            } else if (jo.has("location")) {
                Pattern pattern = Pattern.compile("\\D*([0-9.]+),\\s?([0-9.]+)");
                Matcher matcher = pattern.matcher(jo.getString("location"));

                if (matcher.find()) {
                    lat = Double.parseDouble(matcher.group(1));
                    lon = Double.parseDouble(matcher.group(2));
                }
            }
            if (lat != null) {
                String user = jo.getString("from_user");

                ma = new IconMarker(user + ": " + jo.getString("text"), lat, lon, 0, Color.RED, icon);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ma;
    }
}




Java Source Code List

com.jwetherell.augmented_reality.activity.AugmentedReality.java
com.jwetherell.augmented_reality.activity.AugmentedView.java
com.jwetherell.augmented_reality.activity.Demo.java
com.jwetherell.augmented_reality.activity.SensorsActivity.java
com.jwetherell.augmented_reality.camera.CameraCompatibility.java
com.jwetherell.augmented_reality.camera.CameraModel.java
com.jwetherell.augmented_reality.camera.CameraSurface.java
com.jwetherell.augmented_reality.common.LowPassFilter.java
com.jwetherell.augmented_reality.common.Matrix.java
com.jwetherell.augmented_reality.common.Navigation.java
com.jwetherell.augmented_reality.common.Orientation.java
com.jwetherell.augmented_reality.common.ReusableString.java
com.jwetherell.augmented_reality.common.Vector.java
com.jwetherell.augmented_reality.data.ARData.java
com.jwetherell.augmented_reality.data.BuzzDataSource.java
com.jwetherell.augmented_reality.data.DataSource.java
com.jwetherell.augmented_reality.data.GooglePlacesDataSource.java
com.jwetherell.augmented_reality.data.LocalDataSource.java
com.jwetherell.augmented_reality.data.NetworkDataSource.java
com.jwetherell.augmented_reality.data.PhysicalLocation.java
com.jwetherell.augmented_reality.data.ScreenPosition.java
com.jwetherell.augmented_reality.data.TwitterDataSource.java
com.jwetherell.augmented_reality.data.WikipediaDataSource.java
com.jwetherell.augmented_reality.ui.IconMarker.java
com.jwetherell.augmented_reality.ui.Marker.java
com.jwetherell.augmented_reality.ui.Radar.java
com.jwetherell.augmented_reality.ui.objects.PaintableBox.java
com.jwetherell.augmented_reality.ui.objects.PaintableBoxedText.java
com.jwetherell.augmented_reality.ui.objects.PaintableCircle.java
com.jwetherell.augmented_reality.ui.objects.PaintableGps.java
com.jwetherell.augmented_reality.ui.objects.PaintableIcon.java
com.jwetherell.augmented_reality.ui.objects.PaintableLine.java
com.jwetherell.augmented_reality.ui.objects.PaintableObject.java
com.jwetherell.augmented_reality.ui.objects.PaintablePoint.java
com.jwetherell.augmented_reality.ui.objects.PaintablePosition.java
com.jwetherell.augmented_reality.ui.objects.PaintableRadarPoints.java
com.jwetherell.augmented_reality.ui.objects.PaintableText.java
com.jwetherell.augmented_reality.widget.VerticalSeekBar.java
com.jwetherell.augmented_reality.widget.VerticalTextView.java