Last active
December 21, 2015 07:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dont forget to register RestService in AndroidManifest.xml!