Android Programming Tutorials

(Romina) #1
Raising (Something Like) a Tweet

String responseBody=client.execute(post, responseHandler);
JSONObject response=new JSONObject(responseBody);
}
catch (Throwable t) {
Log.e("Patchy", "Exception in updateStatus()", t);
goBlooey(t);
}
}

private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);

builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}

private View.OnClickListener onSend=new View.OnClickListener() {
public void onClick(View v) {
updateStatus();
}
};
}

Here, we:



  • Set up an DefaultHttpClient instance for accessing the Apache


HttpClient engine



  • Get access to our EditText widgets from the layout

  • On a Send button click, call updateStatus()

  • In updateStatus(), we create an HttpPost object to represent the


request, fill in the authentication credentials, fill in the status as a
form element, turn the whole thing into a valid HTTP POST
operation, execute it, and parse the response as a JSON object

If things work, at present, we do nothing to update the activity; if an


Exception is raised, we log it to the Android log and raise an AlertDialog.


You also need to add the INTERNET permission to your AndroidManifest.xml


file:


161
Free download pdf