Skip to content

Instantly share code, notes, and snippets.

@ursusursus
Last active December 21, 2015 07:38
Show Gist options
  • Save ursusursus/6272129 to your computer and use it in GitHub Desktop.
Save ursusursus/6272129 to your computer and use it in GitHub Desktop.
How to use my ServerUtils "library" as a very simple REST client for Android
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RestService.foo(this, "parameterCisloJedna", new Callback() {
@Override
public void onResult(int status, Bundle results) {
switch (status) {
case Status.OK:
// String version = data.getString("jsonrpc_version");
// LOG.d("Version: " + version);
break;
case Status.ERROR.
// Our server error response
break;
case Status.EXCEPTION:
// Something broke
break;
}
}
});
}
}
public class RestService extends AbstractRestService {
private static final String BASE = "http://moj.super.server.sk";
private static final String URL_FOO = BASE + "/foo.php";
public RestService() {
super(RestService.class.toString());
}
public static void foo(Context context, String someParam, Callback callback) {
String params = new ParamBuilder()
.addParam("some_param", someParam)
.build();
new RequestBuilder()
.setMethod(Methods.POST)
.setUrl(URL_FOO)
.setParams(params)
.setCallback(callback)
.setProcessor(new JsonProcessor() {
@Override
protected void onProcessResponse(Context context, JSONObject json, Bundle results) throws JSONException {
// Extract data from json and post them
// to result bundle
// String version = json.getString("jsonrpc");
// results.putString("jsonrpc_version", version);
}
}).execute(context, RestService.class);
}
}
@ursusursus
Copy link
Author

Dont forget to register RestService in AndroidManifest.xml!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment