Created
December 20, 2011 23:45
-
-
Save 9re/1503842 to your computer and use it in GitHub Desktop.
example of using generics
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
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONTokener; | |
import android.app.Activity; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.loopj.android.http.AsyncHttpResponseHandler; | |
public class JsonTest extends Activity { | |
private TextView mTextView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mTextView = createTextView(); | |
test(); | |
} | |
private void test() { | |
String jsonArray = "[1,\"hello\"]"; | |
String jsonObject = "{\"2\":[1,\"hello\"],\"a\":\"b\"}"; | |
// passing array type works | |
new MyJsonHttpResponseHandler<JSONArray>() { | |
@Override | |
public void onSuccess(JSONArray response) { | |
appendLog("onSuccess", response); | |
} | |
@Override | |
public void onFailure(Throwable throwable, String message) { | |
appendLog("onFailure", throwable, message); | |
} | |
}.handleSuccessMessage(jsonArray); | |
// passing wrong type | |
new MyJsonHttpResponseHandler<JSONArray>() { | |
@Override | |
public void onSuccess(JSONArray response) { | |
appendLog("onSuccess", response); | |
} | |
@Override | |
public void onFailure(Throwable throwable, String message) { | |
appendLog("onFailure", throwable, message); | |
} | |
}.handleSuccessMessage(jsonObject); | |
} | |
private TextView createTextView() { | |
int fillParent = ViewGroup.LayoutParams.FILL_PARENT; | |
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(fillParent, fillParent); | |
TextView textView = new TextView(this); | |
textView.setTextColor(Color.WHITE); | |
addContentView(textView, params); | |
return textView; | |
} | |
private void appendLog(Object...messages) { | |
StringBuilder messageBuilder = new StringBuilder(); | |
for (Object message : messages) { | |
messageBuilder.append(message.toString()) | |
.append(' '); | |
} | |
{// replace the last space with newline | |
int len = messageBuilder.length(); | |
messageBuilder.replace(len - 1, len, "\n"); | |
} | |
mTextView.append(messageBuilder); | |
} | |
private static class MyJsonHttpResponseHandler<JSONType> extends AsyncHttpResponseHandler { | |
public void onSuccess(JSONType response) { } | |
@Override | |
protected void handleSuccessMessage(String responseBody) { | |
super.handleSuccessMessage(responseBody); | |
try { | |
onSuccess(parseResponse(responseBody)); | |
} catch(JSONException e) { | |
// in case, json is malformed | |
onFailure(e, responseBody); | |
} catch (ClassCastException e) { | |
// in case, the expected type is wrong. | |
onFailure(e, responseBody); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
protected JSONType parseResponse(String responseBody) throws JSONException { | |
return (JSONType) new JSONTokener(responseBody).nextValue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment