Multiple Callback Design Pattern? - android

this question is more design related. Am using the Android Async-Http-Client library http://loopj.com/android-async-http/ to make multiple call request from different methods in a class so my code is something like this
RestClient.post(context, "", entity, "application/json", new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Map objects from json using gson
}
});
if different methods in my classes making similar calls like the code Above but with different request params, how can i encapsulate the ResponseHandler Callback so i don't have to keep repeating it in every method call. Was wondering if there is any other nice design pattern method i can use here.
Note
i have thought of subclassing it and as others have suggested, but i can't seem to get the json object response that way.
Thanks

Is the callback behavior identical across all of these requests? If so, you could either create a single shared instance of JsonHttpResponseHandler and use that everywhere:
private JsonHttpResponseHandler handler = new JsonHttpResponseHandler() {
#Override
public void onSuccess(...) {
...
}
}
...
RestClient.post(..., handler);
or you could create a subclass of JsonHttpResponseHandler and use that:
public class MyResponseHandler extends JsonHttpResponseHandler {
#Override
public void onSuccess(...) {
...
}
}
...
RestClient.post(..., new MyResponseHandler());

You dont have to create a new handler for every call.
JsonHttpResponseHandler myHandler = new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Map objects from json using gson
}
};
RestClient.post( context, "", entity, "application/json", myHandler );

How about extracting it as a method:
private void restClients(RestClient restClient){
restClient.post(context, "", entity, "application/json", new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Map objects from json using gson
}
});
}
Then simply calling the method:
restClients(restclient1)

Related

How to get existing payment method from braintree in android?

I have an android client which makes a call to a flask python server like so:
public void getCustomerPaymentMethods() {
Uri.Builder builder = new Uri.Builder()
.scheme("http")
.authority("www.server.appspot.com")
.appendPath("get_customer_payment_methods")
.appendPath(mCustomerId);
String url = builder.build().toString();
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new BaseJsonHttpResponseHandler<List<>>() {
#Override
public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, List<> response) {
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, List<> errorResponse) {
// Display UI for no payment methods
mNoPaymentMethodImageView.setVisibility(View.VISIBLE);
mAddPaymentMethodButton.setVisibility(View.VISIBLE);
}
#Override
protected List<> parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
return null;
}
});
}
This is the server side code mapped to the url provided:
#app.route('/get_customer_payment_methods/<cutomerId>')
def get_customer_payment_methods(customerId):
result = braintree.Customer.find(customerId)
return result.payment_methods
What object do I have to save the response from the python server in on the client side?

LoopJ AndroidAsyncHttp didn't go to onSuccess() or onFailure()

I used LoopJ AndroidAsyncHttp to get the response from the url, but the code didn't go into onSuccess() or onFailure(). The code is as below:
public void queryTopic(RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.0.109:8080/PhoneServer/topic/query", params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
System.out.println("It's in onSuccess");
}
// When the response returned by REST has Http response code
// other than '200'
#Override
public void onFailure(int statusCode, Throwable error,
String content) {
System.out.println("It's in onFailure");
}
});
System.out.println("It's over");
}
It just printed out the "It's over". What's the matter with the AsyncHttpClient?
How are you calling this? Is the context alive? If you are calling it from some place where context is no more available, then you will never get this callbacks.
I think you should try calling this queryTopic() method on click of some button and then wait for some time, you should get the response.
maybe the problem is that you ask to the server for a String
public void onSuccess(String response){...}
but server answer with a JSONObject
You can try this code:
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(5000);
client.get(yourActivity.class, yourLink, new JsonHttpResponseHandler(){
#Override
public void onStart() {
Log.e(TAG, "start");
}
#Override
public void onSuccess(int status, Header[] headers, JSONObject answer) {
Log.e(TAG, "SUCCESS");
Log.e(TAG, "print => "+answer.getString("answer_id")); //"answer_id" is a random example
}
#Override
public void onFailure(int status, Header[] headers, String answer, Throwable throwable) {
Log.e(TAG, "FAILURE");
}
});
onSuccess() is an overloaded method be careful regarding what you send from server,
if it is a jsonObject or jsonArray or simple string. Use corresponding overloaded method of onSuccess(). I am using it too for a jsonObject response and i confront no error or irregularities in the method.
I return jsonObject from server for which my code works as expected and is as follows:
RequestParams params = new RequestParams();
params.put("pet","Cat");
params.put("name","Maran");
RestClient.get("/savelocation", params, new JsonHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Toast.makeText(context,response.toString(),Toast.LENGTH_SHORT).show();
Log.e("Error makerequest","request completed");
}
#Override
public void onFinish() {
//onLoginSuccess();
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable,JSONObject errorResponse){
Toast.makeText(context,throwable.toString(),Toast.LENGTH_LONG).show();
}
});
Note: RestClient is a static instance of AsyncHttpClient

Android async httpclient: how to intercept authentication failures in one place?

I have the following mental model that for my android app, all authentication failures should be handled in a single place and the login screen should interrupt and be flashed on top of the view stack to allow the user to reauthenticate.
Not sure how to do this with AsyncHTTPClient.
I do not want to keep handling the 401 and 404 requests for every single GET and POST request that I make. Sounds like a lot of repeated code. So for google and CNN, for example, if I am not authenticated, I don't want to repeat the code to handle the 401 not authenticated code for both CNN and google because it's the same exact code.
Thank you!
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);if 401, TELL USER he has to log in again
}
});
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.cnn.com", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);if 401, TELL USER he has to log in again
}
});
create your own subclass for AsyncHttpResponseHandler and implement the onFailure alone; Add all your failure handling logic in there.
Use this subclass for your client.get instead of AsyncHttpResponseHandler, just implementing the onSuccess.
client.get("http://www.cnn.com", new MyAsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
// onFailure handled by common code in MyAsyncHttpResponseHandler
}

LoopJ Android Asynchronous Http Client onpostexecute?

I am making a GET call and am able to add all the results into an array after parsing the JSON. I want to use that array onpostexecute call. Can I do that with this library?
I do it in this way , hope it will help you.
in bussiness layer
HttpUtils.getJson(url, null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
}
});
in HttpUtils.java
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) //url里面带参数
{
client.get(urlString, params, res);
}

Android: Loopj AsyncHttpClient Get HTTP Error Code

I'm using Loopj's AsyncHttpClient to do http requests with JSON data. Now I need to get the http error code that is returned from the server. Where can I find that?
The handler that I pass looks like this:
new JsonHttpResponseHandler()
{
#Override
public void onFailure(Throwable throwable, JSONObject object)
{
// Get error code
}
}
Is this not possible at all?
Try this:
HttpResponseException hre = (HttpResponseException) throwable;
int statusCode = hre.getStatusCode();
It should work only for status code >= 300, because of following code in AysncHttpResponseHandler class in loopj
if(status.getStatusCode() >= 300) {
sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
}
Give a look at client.post(null, url, entity, "application/json", responseHandler);
Declare method as given in below and you will you will get status code
RestClientAvaal.post(url, entity, new BaseJsonHttpResponseHandler<>() {
#Override
public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) {
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) {
}
#Override
protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
return null;
}
});
And if you want to get exception message then try new Exception(throwable).getMessage() in onFailure method

Categories

Resources