Processing Android with Ushahidi

For my project, Nestless, I used Processing Android to create a front-end interface to interact with Ushahidi. Now, there are several reasons why this is advantageous:

1. Processing Android allows for unique interfaces that are entirely customizable, which can be crucial when designing a UI that best fits certain projects. For example, in my app, I have a bird that flies to a certain spot on a custom map.

2. Processing Android provides opportunities for rich visualization; web application do not.

3. This allows full control over the user experience. This can be crucial for software that asks users to contribute time to map something.

Of course, this is limited to Android phones. In addition, it can take a bit more time to create. But if you can get over that, this is a great opportunity to marry the rich visual potential of Processing with the robust and easy-to-use backend of Ushahidi.

Before you go on, you may want to look over my post on Nestless, just to get an idea of what I’ve used this for. (It’s a super simple implementation, with more to come.)

So here are some simple instructions on how to do it:

1. Create a Ushahidi or Crowdmap site: (Crowdmap is simply the hosted version.) Ushahidi basically serves as a mapping service. You send information to Ushahidi, and it will map it for you — and you don’t have to deal with databases or creating a website or how to parse your data and display it in a useful way.

Anyway, I recommend creating this page first because helps you understand what kind of information you want to collect from your app. Basically, Ushahidi needs a handful of things from your Android app:

> Report title
> Report description
> Category
> Date/time
> Location: latitude and longitude

2. Create your Processing for Android app: I wrote up a quick post on how to install Processing for Android.

3. How to create text boxes: Processing for Android doesn’t make it easy to create text boxes for users. However, a nice library to use here is APWidgets. After you download the library and put it in your library folder, type the follow at the top of your code:

import apwidgets.*;
import android.text.InputType;
import android.view.inputmethod.*;

In addition, on your menubar, go to “Sketch” > “Add file…” and navigate to your Library folder. From there, find “apwidgets” > “library” > “apwidgets.jar” — and click open.

From there, you need to create the object for the container and well as the text fields. So I did something like this (the apwidgets site has some nice examples):

APWidgetContainer container;
APEditText nameField, descriptionField;

And in the setup, make sure to initiate it:

container = new APWidgetContainer( this );

nameField = new APEditText( 10, 100, width-20, 70 ); //sets location of text field
nameField.setInputType(InputType.TYPE_CLASS_TEXT); //set the input type to text
nameField.setImeOptions(EditorInfo.IME_ACTION_DONE); //enables done button
nameField.setCloseImeOnDone(true); //close the IME when done is pressed

descriptionField = new APEditText( 10, 100, width-20, 150 );
descriptionField.setInputType(InputType.TYPE_CLASS_TEXT);
descriptionField.setImeOptions(EditorInfo.IME_ACTION_DONE);
descriptionField.setCloseImeOnDone(true);
descriptionField.setHorizontallyScrolling(false);

There are lots of things you can customize. Just mess around with the examples, and you’ll figure it out.

Now, you may notice that you don’t actually want the text boxes on the screen at all times. So the way to deal with it is by adding the “nameField” when I want it and removing it when I’m done with it. Here’s how to do that:

if (I_want_to_add == true) {
container.addWidget(nameField);
} else {
container.removeWidget(nameField);
}

Lastly, if you want to set text into your fields, use this function:

nameField.setText("Here's some text");

4. Other data input: You can use basic buttons to help users input data, as well. (The mouseReleased() function is a great way around using touch function, although the Processing for Android website gives you some other great options of how to deal with the touch screen.)

Just remember to store all your data in variables.

5. Location data: Here’s are the examples to help get the GPS data from your phone. It’s easy; trust me.

6. Do you have all your data? So make sure that by the time you get to a “submit” button, all the data you want to send is stored in variables. Good? Yeah, of course you are.

7. Create PHP file: Now, it’s best if Processing talks to a PHP script, which then talks to Ushahidi. So here’s how that works.

First, create an empty PHP file and put it on your server. Let’s assume you uploaded your php file at: http://rainbowchang.com/phpfile.php

8. Sending the variables to PHP: Now, go back to Processing and we can start gathering a string to load to your php script. By that, I mean we can compile all your variables, combine it into one long url and load it. Here’s how that works:

String thing = "http://rainbowchang.com/phpfile.php?name=" + name + "&description=" + description + "&longitude=" + currentLongitude + "&latitude=" + currentLatitude;
loadStrings(thing);

So when do you run this code? Well hopefully you’ll have something like a “Submit” button. So you can say:

if (submitted == true) {
String thing = "http://rainbowchang.com/phpfile.php?name=" + name + "&description=" + description + "&longitude=" + currentLongitude + "&latitude=" + currentLatitude;
loadStrings(thing);
}

Might freeze while it sends, so beware.

Before we go on, a bit about urls. Lpok at the String “thing”. Everything after the ‘?’ are “paramaters.” And in this case, these are basically passing variables to the php script. So if the “name” variable we’re passing through is “Alvin” and the description is “coolperson”, then it would be “http://rainbowchang.com/phpfile.php?name=Alvin&description=coolperson”. Notice how the two variables in the url are divided with the ampersand (‘&’).

The next part of this is the “loadStrings(thing)” which basically runs that php script on your server, while passing your variables to it.

But wait! It won’t work yet. Your php script is empty because I just made you create a blank file. Let’s fix that.

9. Editing PHP file: Go to your php file on your server and edit it like this:

<html>
<head>
<title>PHP Page</title>
</head>
<body>

<?

$name = $_GET["name"];
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];
$description = $_GET["description"];
$incidentdate = date("m/d/Y");
$incidenthour = date("g");
$incidentmin = date("i");
$incidentam = date("a");
$category = 0;
?>
</body>
</html>

(Note: All PHP code by go between the ‘‘)

Now, notice how the word inside the “$_GET[]” functions mirror the variables that we passed to it in Processing in step No. 7. Well, this php script is basically grabbing the variables from the parameters you’ve passed through. So remember how we talked about this url: “http://rainbowchang.com/phpfile.php?name=Alvin&description=coolperson”?

Well now the PHP script is reading it like this:

$name = "Alvin";
$description = "coolperson";

(Also notice: PHP variables have a dollar sign in front of them. If you have trouble with PHP syntax, just Google it. It’s easy as pie.)

OK, so now you’ve passed through all your data. You should figure out how it determines what category it will be. So you can say:

if ($name == "Alvin") {
$category = 1;
}

You can set your categories on your Ushahidi settings page, which you can access via your dashboard.

Now you should be ready to use the Ushahidi API.

10. The Ushahidi API: This is how you’re sending your data from PHP to Ushahidi.

Take the code below and replace the URL. Give what we’ve set up previously, the variables should be named the same below. But if needed, edit the variable names that you’re sending to Ushahidi:


if ($name == "Alvin") {
$category = 1;
}

$posturl = &quot;https://yourUshahidiURL.com&quot;; //YOUR USHAHIDI URL HERE
$Curl_Session = curl_init($posturl);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);

curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, &quot;task=report&amp;incident_title=$name&amp;incident_description=$description&amp;incident_date=$incidentdate&amp;incident_hour=$incidenthour&amp;incident_minute=$incidentmin&amp;incident_ampm=$incidentam&amp;incident_category=$category&amp;latitude=$latitude&amp;longitude=$longitude&amp;location_name=Unknown&quot;); //You may need to edit this line with the correct variable names

curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);

11. Entire PHP Script: It might look something like this, in total:

<?

$name = $_GET["name"];
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];
$description = $_GET["description"];
$incidentdate = date("m/d/Y");
$incidenthour = date("g");
$incidentmin = date("i");
$incidentam = date("a");
$category = 0;

$posturl = "https://yourUshahidiURL.com"; //YOUR USHAHIDI URL HERE
$Curl_Session = curl_init($posturl);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);

curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "task=report&incident_title=$name&incident_description=$description&incident_date=$incidentdate&incident_hour=$incidenthour&incident_minute=$incidentmin&incident_ampm=$incidentam&incident_category=$category&latitude=$latitude&longitude=$longitude&location_name=Unknown"); //You may need to edit this line with the correct variable names

curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>

12. Done! Now go to your Processing app, run through it and you should be able to send those variable to Ushahidi — and it should be mapped on your map URL. Just go adjust some info on your Ushahidi settings page, but that’s not a huge deal.

Please feel free to ask any questions or point out better ways/errors here.

45 responses to “Processing Android with Ushahidi”

  1. Nootropic

    This writing has inspired me to carry on working on my own blog

  2. tiles new south wales

    Greeting from across the sea. precise article I must return for more.

  3. Pedro Rivera

    Hi, I find this guide very useful, but I I’m using java instead of processing. What would be an equivalent to loadStrings() in java?

    I found other places online that set $posturl to http://ushahidiInstallation.com/api, why is that?

    Thanks!

    1. Alvin

      Hi Pedro,

      Unfortunately, I don’t know what the Java equivalent is. Sorry :/

      For $posturl, I’d say try both and see which works. This setup worked for me, but test out both 🙂

      Thanks!

  4. google advertising tools

    You actually make it seem so easy with your presentation but I find this matter
    to be really something which I think I would never understand.
    It seems too complicated and extremely broad for me.

    I am looking forward for your next post, I will try to get the
    hang of it!

  5. Rat Exterminator

    We’re a bunch of volunteers and opening a brand new scheme in our community.
    Your web site provided us with helpful info to work on. You have done a formidable
    activity and our entire neighborhood might be thankful to you.

  6. wolfenstein the new order free download pc

    As with anything such as the original Nintendo Entertainment System
    started the generation of video games present special problems,
    wolfenstein the new order download in the case.

  7. download dayz

    After going over a handful of the articles on your site, I seriously
    appreciate your way of writing a blog. I book-marked it to my bookmark webpage list and will be checking back soon.
    Take a look at my website as well and let me know
    what you think.

  8. dentist reviews Wake Forest NC

    excellent submit, very informative. I ponder why the other specialists of this sector do not realize this.
    You must proceed your writing. I’m sure, you’ve a great
    readers’ base already!

  9. internet business opportunities for

    Valuable info. Fortunate me I discovered your website by chance, and I am surprised why this coincidence didn’t
    came about in advance! I bookmarked it.

  10. tree companies Birmingham AL

    I am in fact grateful to the holder of this
    web site who has shared this fantastic piece of writing at here.

  11. physical therapy in los Angeles

    Greetings! I know this is kinda off topic but I’d figured I’d ask.

    Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
    My site addresses a lot of the same subjects as yours and
    I feel we could greatly benefit from each other.
    If you are interested feel free to send me an e-mail.
    I look forward to hearing from you! Fantastic blog by the way!

  12. quick money

    Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is magnificent,
    as well as the content!

  13. best video creation software

    I’m really impressed with your writing skills as well as with the
    layout on your weblog. Is this a paid theme or did you
    modify it yourself? Anyway keep up the excellent quality writing,
    it’s rare to see a nice blog like this one these days.

  14. bio identical hormones

    Link exchange is nothing else however it is only placing the other person’s weblog link
    on your pwge at suitable place and other person will also
    do same in sulport of you.

  15. Austin trash removal service

    Marvelous, what a webpage it is! This weblog provides helpful data
    to us, keep it up.

  16. surety bond underwriter

    Hi, i think that i saw you visited my weblog thus
    i came to “return the favor”.I’m attempting to find things to
    improve my site!I suppose its ok to use some of your ideas!!

  17. International Shipping Canada Sumas

    What i do not understood is in truth how you are not
    really much more neatly-favored than you may be now.
    You’re so intelligent. You realize thus significantly in relation to this subject,
    produced me individually imagine it from numerous numerous angles.

    Its like women and men aren’t interested except it’s one thing to do with Woman gaga!
    Your personal stuffs nice. All the time deal with it
    up!

  18. https://scrapbooking.zendesk.com/

    I like the valuable info you provide in your
    articles. I will bookmark your blog and check again here regularly.
    I am quite certain I’ll learn a lot of new stuff right here!
    Good luck for the next!

  19. Porfirio

    Aw, this was an incredibly nice post. Taking the
    time and actual effort to generate a great article… but what can I say… I put things off a lot and never manage to get
    nearly anything done.

  20. Annetta

    I know this website provides quality based articles and additional information, is there any other web page which presents such information in quality?

  21. Speed Painter

    Greetings! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for
    my comment form? I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  22. olympics twitter

    I do not know if it’s just me or if everybody else encountering issues with
    your website. It appears like some of the text on your content are running off the screen. Can somebody else please provide feedback and
    let me know if this is happening to them too?
    This may be a problem with my internet browser because I’ve had this happen before.
    Appreciate it

  23. washington heights dentista

    I was recommended this website by my cousin. I am not sure
    whether this post is written by him as no one else know such detailed about my
    difficulty. You are wonderful! Thanks!

  24. Yoga Inwood NYC

    I don’t even understand how I stopped up right here, however
    I thought this publish was good. I don’t recognize who you’re but certainly you’re going to a
    famous blogger in case you are not already. Cheers!

  25. Run Click Software by Walter Bayliss

    If you are going for most excellent contents like me, just visit this site daily since it provides quality contents, thanks

  26. is seamless secure worth buying

    I’ve been browsing online greater than 3 hours lately,
    but I by no means discovered any interesting article like yours.
    It’s beautiful price sufficient for me. In my view, if all
    web owners and bloggers made good content material as you did,
    the web will be much more helpful than ever before.

  27. root canal removal Elk Grove

    Its not my first time to pay a visit this website,
    i am visiting this web site dailly and get good information from here daily.

  28. Residential Pest Control Company Chilliwack

    Hey just wanted to give you a brief heads up and let you know a few of the images aren’t
    loading correctly. I’m not sure why but I think its
    a linking issue. I’ve tried it in two different web browsers and both show the
    same results.

  29. Animal Removal Wildlife Vancouver

    I couldn’t resist commenting. Perfectly written!

  30. hamptonroads.com

    It’s awesome for me to have a web site, which is beneficial in favor of my know-how.
    thanks admin

  31. butt foods

    each time i used to read smaller articles which
    as well clear their motive, and that is also happening with this article which I am reading at this
    time.

  32. temple run 2 hack

    How exactly to total level in Temple Work two?

  33. League of legends helper

    I visited various websites however the audio quality for audio songs present att this weeb site is relly wonderful.

  34. Rosio L. Clausing

    I enjoy what you guys tend to be up too. This kind of clever work and exposure!
    Keep up the excellent works guys I’ve added you guys to my personal blogroll.

  35. Elisa A. Fredericksen

    Wow, that’s what I was seeking for, what a stuff!

    present here at this blog, thanks admin of this site.

  36. www.youtube.com

    I got this web site from my buddy who informed me regarding this web page and now this time
    I am browsing this site and reading very informative articles here.

  37. Callum

    Hi,

    Great article!! However I am having trouble getting the add/remove widget part to work in processing, I am using more than one page so want to be able to add and remove for each page. Whereabouts do you put your if statement for adding and removing the widget (i.e draw() or somewhere else)?

    Thanks

  38. black anddecker juicer

    Pimms is Cap Jaluca’s award winning restaurant and holds its weight
    in ambiance and outstanding cuisine amongst the finest in the Caribbean. There
    are many different ways to enjoy the wonder of a Disney holiday.
    There are a total of nine unique habitats where you may discover many
    different types of wildlife animals and over one hundred species of birds that
    you may enjoy seeing while on your Florida vacation.

  39. mima.lt beau loves

    To secure the shape with a t-shirt. To 1:00 p m. And they beau love drabuziai look
    very good condition. To 8:00 p m. Well Kimberly, I put it in, there
    is still, yet, said his mother.The one I’m wearing is the
    limit. Don’t worry, though enthusiastic, are those ffrom The Children’s Place is another option tooo is jjust what every bride desires.
    Hell no, no, no, all of them don’t even need to open it, and it’s just gross.

  40. mortgages

    I will show you several wealth destroying emotional conflicts that must be resolved if you are to acquire personal wealth.
    Who do you contact for live assistance from someone who has the
    knowledge to assist you. Start spending them – you
    can take some to get something from the vending machine, help out someone who needs change,
    or buy an item from the store.

  41. quick weight loss

    For the proper consistency about 1 cup of water should be added to the ground peel.
    Don’t worry about them; just talk to your pharmacist
    or doctor if you are not already using some creams.
    Another habit you have to apply is avoiding touching your face especially with dirty hands because you may transfer bacteria which may cause acne.

  42. jacks blow job lessons

    Please do not assume that a person with acne is “dirty”.
    Now gently rub it on the scar in circular motion before rinsing with
    cold water. If you would like to remove them naturally, it is important for you to check
    this article out that gives better information on how
    to solve the problems of acne scars naturally.

  43. highperformancehandbookpdf.blogspot.com

    Quality posts is the important to invite the people to go to see the website, that’s what this website is providing.

  44. Arreglo de nevera madrid

    PERSONAL OFICIAL DE PROFESIÓN con experiencia en los sectores de trabajo a desarrollar garantizando una reparación eficaz y de garantía. SERVICIOS PRESTADOS EN LA COMUNIDAD DE MADRID, en MADRID ciudad NO COBRAMOS EL DESPLAZAMIENTO, presupuestos de aire aconcicionado y calefacción, calderas de todas las marcas. Su Lavadoras Siemens quedará como nuevo después de la reparacion de nuestro servicio tecnico Siemens.

Leave a Reply