I’m spinning my wheels here trying to get AsyncTask to work. I’ve got a method that connects to a web service and then sets my textview based on the response code. The method looks like this:
private void connect() throws OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException, IOException {
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
consumer.setTokenWithSecret("", "");
// create an HTTP request to a protected resource
URL url = new URL("http://blablabla.com/bla.json");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
// sign the request
consumer.sign(request);
// send the request
request.connect();
Log.i("Pimpshit", "Response code is: " + request.getResponseMessage());
if(request.getResponseCode()==200) {
mText.setText("Sorry, failed to connect to X");
} else if(request.getResponseCode()==401) {
mText.setText("Congrats, you're connected to X!");
} else
mText.setText("Whatever you're asking for, it ain't a valid HTTP request...");
}
How do I fire off an AsyncTask from onCreate to do this?
First, I would generalize the call to GetData for code reuse as:
Second I would make getData No Throws perhaps by wrapping getData in try catch and returning a custom object on exception or on success
as
Then I would create an inner class:
and call it as in: