I recently had the need to call a REST based web service from Android. I
searched the net for several days before finding the proper (and
easiest) way to do this. Hopefully I will save some of you the trouble.
First you will need to import all of the apache HttpClient libs. Then
you create the request and make the call, the result of which is a
String. In My example, I am merely making the call after the user
chooses to search for a specific value.
package com.example.android;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class AndroidApp extends Activity {
String URL = "http://the/url/here";
String result = "";
String deviceId = "xxxxx" ;
final String tag = "Your Logcat tag: ";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);
txtSearch.setOnClickListener(new EditText.OnClickListener(){
public void onClick(View v){txtSearch.setText("");}
});
final Button btnSearch = (Button)findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String query = txtSearch.getText().toString();
callWebService(query);
}
});
} // end onCreate()
public void callWebService(String q){
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL + q);
request.addHeader("deviceId", deviceId);
ResponseHandler handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
Log.i(tag, result);
} // end callWebService()
}
Today is the last day of PyCon 2010 and I am waiting on the next talk to begin. I’ve learned so much that, at times, I thought my head might just explode. Sessions run until 5pm, with Open Space sessions often running past 10pm. There are so many really smart people here and it is easy to get caught up in a conversation with someone you met just moments ago.
I think coming to PyCon may have been one of the best things I have done for myself in some time. It’s truly and amazing language and coming to a conference like this just lets me know that I am on the right path. I got the great chance to meet and speak with the folks doing the Jython work and that may be the way for me to do more python at work, considering I work for an almost 100% Java shop. Most of all, I take away from this that I need to get more involved in the community and do even more than I do. To get hired doing python, it’s probably best to get your name out there and be active. The community is small enough that it really shouldn’t be that hard to get your name known. So that is my goal for the next year, get my name known. Part of this will be running the Birmingham Users Group, part if it will be maybe helping with documentation and such for Python.org, part will be getting involved and coding on some open source projects.
Serendipitously, it seems PyCon 2011 will be held in Atlanta again next year. I will surely be here, regardless of what it takes to make that happen.
Lessons learned: If you really care about software as more than just your day job, get involved with the open source community in whatever language you have chosen. Take part in your local users groups. Get your name out there and do something to be a part of the community. Attend a conference, especially if it is one that is small enough that you can feel significant. If you are still searching for one, let me suggest the Python community. It really does rock!
Looking back, I have generally been the type of person who lets people dictate to me what it is that I am going to do. I make my desires known, but don’t do anything to make it a point that my desires will be the outcome. Today I took a big step and stood up for myself. After a couple of years of being led to believe that I would finally move into a full time development position, and then being stuck in a designer/analyst position, I laid it all out on the table. I let my boss know that either we start making the move, or I would need to look at other opportunities.
I’m very happy to report that it actually went over well. As I understand, we will be developing a plan to train someone to start stepping into my position as I start stepping into a much more development focused role. It was the perfect time to make the move as I am currently needed and we are about to be in a position that we will be short on developers. I’ll continue to post and update on how things go over the next few weeks. I’m tired of getting stepped on and ready to take my destiny in my own hands.
Only two more weeks left until PyCon! I’m excited to get out and see the greater Python community.
Lesson learned: You don’t get anywhere without taking risk. It was really scary to make a play like I did today, but the outcome may be worth it.
I’ve just found out that my annual review will be next Wednesday. I’ve been looking forward to this. I rarely get the chance to sit down with upper management and discuss my goals, at least in a setting where I feel like anyone may care. I’ve officially requested a re-titling to “Programmer/Business Analyst”. I don’ t believe my current title reflects all of what I do. While it may not actually mean much in the end, it affects the way others view me. I’m hoping to get a mandate to do more coding as part of this title change. This is the most direct I’ve been about this, but after almost a year and a half since I got my certification, it’s getting pretty old. I’m trying to find a way to stand up for myself without making the wrong impression.
Wish me luck, I’ll update Wednesday evening with the results!
I’ve added a section to the Portfolio page to include sys admin scripts I’ve written, and used. I started by adding a quick script that can be run from cron. It will check your external IP against a website and let you know, via email, if it has changed. The local file is ip.txt and should be saved in your home directory if running from cron.