The Web API

A sample API to reverse string:

It takes a input parameter: str, then reverse it.

The conent of the reverse.php:

$msg = $_REQUEST['str'];

$data = array();

if (!$msg) {
    $data['ok'] = 0;
} else {
    $data['ok'] = 1;

$data['str'] = $msg;
$data['result'] = strrev($msg);
$data['server_time'] = date('Y-m-d H:i:s');
echo json_encode($data);

A sample request:

The output json:

    "ok": 1,
    "str": "123",
    "result": "321",
    "server_time": "2014-02-14 23:56:25"

Encapsulate Reuqest

It is a good practice to encapsulate all the request logic in a module, for example:

public class SampleRequest {

     * Show how to encapsulate the calling of a web API by Request
    public static void reverse(final String str, final JsonRequestSuccHandler handler) {
        new SimpleRequest<JsonData>(new BeforeRequestHandler() {

                public <T> void beforeRequest(SimpleRequest<T> request) {

                String url = "" + str;

                }, handler).send();

The UI

Request Demo

Click the button in the buttom, fetch the data, then display the data.

The controls:

final EditText inpuText = (EditText) view.findViewById(;
final TextView okTextView = (TextView) view.findViewById(;
final TextView resultTimeTextView = (TextView) view.findViewById(;
final TextView serverTimeTextView = (TextView) view.findViewById(;
final TextView button = (TextView) view.findViewById(;

button.setText("Click to request");

In the following code, you can see it is realy very simple to call the web API and use the data.

OnClickListener onClickListener = new OnClickListener() {

    public void onClick(View v) {


        String str = inpuText.getText().toString();

        SampleRequest.reverse(str, new JsonRequestSuccHandler() {

                public void onRequestFinish(JsonData jsonData) {

                button.setText("Click to request");