use of com.google.gson.reflect.TypeToken in project instructure-android by instructure.
the class AvatarUnitTest method test1.
@Test
public void test1() {
// /users/self/avatars
final Gson gson = CanvasRestAdapter.getGSONParser();
final List<Avatar> list = gson.fromJson(JSON, new TypeToken<List<Avatar>>() {
}.getType());
for (Avatar a : list) {
assertNotNull(a);
assertNotNull(a.getDisplayName());
assertNotNull(a.getUrl());
assertNotNull(a.getToken());
assertNotNull(a.getType());
}
}
use of com.google.gson.reflect.TypeToken in project instructure-android by instructure.
the class OAuthWebLogin method eatSnickerDoodles.
/**
* Adds a simple login method for devs. To add credentials add your snickers (credentials) to the snickers.json
* Slide the drawer out from the right to have a handy one click login. FYI: Only works on Debug.
* Sample Format is:
*
* [
* {
* "password":"password",
* "subtitle":"subtitle",
* "title":"title",
* "username":"username"
* },
* ...
* ]
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private void eatSnickerDoodles() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
Writer writer = new StringWriter();
try {
InputStream is = getResources().openRawResource(getResources().getIdentifier("snickers", "raw", getPackageName()));
char[] buffer = new char[1024];
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
is.close();
} catch (Exception e) {
// Do Nothing
}
String jsonString = writer.toString();
if (jsonString != null && jsonString.length() > 0) {
ArrayList<SnickerDoodle> snickerDoodles = new Gson().fromJson(jsonString, new TypeToken<ArrayList<SnickerDoodle>>() {
}.getType());
if (snickerDoodles.size() == 0) {
findViewById(R.id.drawerEmptyView).setVisibility(View.VISIBLE);
findViewById(R.id.drawerEmptyText).setVisibility(View.VISIBLE);
return;
}
web.getSettings().setDomStorageEnabled(true);
web.setWebChromeClient(new WebChromeClient());
mDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true));
mDrawerRecyclerView.setAdapter(new SnickerDoodleAdapter(snickerDoodles, new SnickerDoodleAdapter.SnickerCallback() {
@Override
public void onClick(SnickerDoodle snickerDoodle) {
mDrawerLayout.closeDrawers();
final String js = "javascript: { " + "document.getElementsByName('pseudonym_session[unique_id]')[0].value = '" + snickerDoodle.username + "'; " + "document.getElementsByName('pseudonym_session[password]')[0].value = '" + snickerDoodle.password + "'; " + "document.getElementsByClassName('btn')[0].click(); " + "};";
web.evaluateJavascript(js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
final String js = "javascript: { " + "document.getElementsByClassName('btn')[0].click();" + "};";
web.evaluateJavascript(js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
}
});
}
});
}
}, 750);
}
}));
} else {
findViewById(R.id.drawerEmptyView).setVisibility(View.VISIBLE);
findViewById(R.id.drawerEmptyText).setVisibility(View.VISIBLE);
}
}
use of com.google.gson.reflect.TypeToken in project Terasology by MovingBlocks.
the class BehaviorTreeBuilder method getCompositeNode.
private BehaviorNode getCompositeNode(JsonElement json, JsonDeserializationContext context) {
String type;
JsonObject obj = json.getAsJsonObject();
Map.Entry<String, JsonElement> entry = obj.entrySet().iterator().next();
type = entry.getKey();
JsonElement jsonElement = entry.getValue();
BehaviorNode node = createNode(type);
if (actions.containsKey(type)) {
Action action = context.deserialize(jsonElement, actions.get(type));
addAction((ActionNode) node, action);
} else if (decorators.containsKey(type)) {
Action action = context.deserialize(jsonElement, decorators.get(type));
addAction((ActionNode) node, action);
JsonElement childJson = jsonElement.getAsJsonObject().get("child");
BehaviorNode child = context.deserialize(childJson, BehaviorNode.class);
node.insertChild(0, child);
} else if (jsonElement.isJsonArray()) {
List<BehaviorNode> children = context.deserialize(jsonElement, new TypeToken<List<BehaviorNode>>() {
}.getType());
for (int i = 0; i < children.size(); i++) {
BehaviorNode child = children.get(i);
node.insertChild(i, child);
}
}
return node;
}
use of com.google.gson.reflect.TypeToken in project YhLibraryForAndroid by android-coco.
the class JsonUitl method parseJsonArrayWithGson.
/**
* 将Json数组解析成相应的映射对象列表
* @param jsonData
* @param cls
* @param <T>
* @return
*/
public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> cls) {
Gson gson = new Gson();
List<T> result = gson.fromJson(jsonData, new TypeToken<List<T>>() {
}.getType());
return result;
}
use of com.google.gson.reflect.TypeToken in project vorto by eclipse.
the class DefaultModelPublisher method publish.
@Override
public ModelId publish(ModelType type, String content) throws ModelPublishException {
String uploadModelsUrl = String.format("%s/rest/secure", getRequestContext().getBaseUrl());
HttpPost query = new HttpPost(uploadModelsUrl);
HttpEntity entity = MultipartEntityBuilder.create().addPart("fileName", new StringBody("vortomodel" + type.getExtension(), ContentType.DEFAULT_TEXT)).addPart("fileDescription", new StringBody("", ContentType.DEFAULT_TEXT)).addPart("file", new ByteArrayBody(content.getBytes(), ContentType.APPLICATION_OCTET_STREAM, "vortomodel" + type.getExtension())).build();
query.setEntity(entity);
try {
CompletableFuture<UploadModelResponse> response = execute(query, new TypeToken<UploadModelResponse>() {
}.getType());
List<UploadModelResult> result = response.get().getObj();
if (response.get().getIsSuccess()) {
String checkinModelUrl = String.format("%s/rest/secure/%s", getRequestContext().getBaseUrl(), result.get(0).getHandleId());
HttpPut checkInModel = new HttpPut(checkinModelUrl);
CompletableFuture<ModelId> checkedInResult = execute(checkInModel, new TypeToken<ModelId>() {
}.getType());
return (ModelId) checkedInResult.get();
} else {
throw new ModelPublishException(result.get(0));
}
} catch (Throwable ex) {
if (!(ex instanceof ModelPublishException)) {
throw new RuntimeException(ex);
} else {
throw ((ModelPublishException) ex);
}
}
}
Aggregations