List of usage examples for com.squareup.okhttp Response body
ResponseBody body
To view the source code for com.squareup.okhttp Response body.
Click Source Link
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Update an existent device//www . j av a 2 s . c o m * * @param device the identifier of device (uuid) * @return the object updated * @throws KnotException KnotException */ public <T extends AbstractThingDevice> T updateDevice(String id, T device) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Update the given device final String endPoint = mEndPoint + DEVICE_PATH + id; String json = mGson.toJson(device); RequestBody body = createRequestBodyWith(json); Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).put(body).build(); try { Response response = mHttpClient.newCall(request).execute(); return (T) mGson.fromJson(response.body().string(), device.getClass()); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get all information regarding the device. * * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an json element containing device informations * @throws KnotException KnotException//from w w w .ja v a2 s . c o m */ public <T extends AbstractThingDevice> T whoAmI(Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get all information regarding the given device final String endPoint = mEndPoint + WHOAMI; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); return mGson.fromJson(jsonElement.toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get a specific device from Meshblu instance. * * @param device the device identifier (uuid) * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an object based on the class parameter * @throws KnotException KnotException/*from w w w . j a va2s . c om*/ */ public <T extends AbstractThingDevice> T getDevice(String device, Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get the specific device from meshblue instance final String endPoint = mEndPoint + DEVICE_PATH + device; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); if (jsonArray.size() == 0) { return null; } return mGson.fromJson(jsonArray.get(0).toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get a specific device's gateway from Meshblu instance. * * @param device the device identifier (uuid) * @param clazz The class for this device. Meshblu works with any type of objects and * it is necessary deserialize the return to a valid object. * Note: The class parameter should be a extension of {@link AbstractThingDevice} * @return an object based on the class parameter * @throws KnotException KnotException//from www . ja va2s. c om */ public <T extends AbstractThingDevice> T getDeviceGateway(String device, Class<T> clazz) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get a specific device's gateway from Meshblu instance. final String endPoint = mEndPoint + DEVICE_PATH + device + DEVICE_PROPERTY_PATH_GATEWAY; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); if (jsonArray.size() == 0) { return null; } return mGson.fromJson(jsonArray.get(0).toString(), clazz); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Get all devices those are claimed by one owner * * @param type object that will define what elements will returned by this method * @return a List with all devices those belongs to the owner * @throws KnotException KnotException//from w ww. ja va 2 s . c om */ public <T extends AbstractThingDevice> List<T> getDeviceList(final KnotList<T> type) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } // Get all devices those are claimed by one owner final String endPoint = mEndPoint + MY_DEVICES_PATH; Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.code() != HttpURLConnection.HTTP_OK) { return null; } JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DEVICES); return mGson.fromJson(jsonArray.toString(), type); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * @param device the device identifier (uuid) * @param type object that will define what elements will returned by this method * @param knotQueryData Date query/*from w w w. jav a 2s. co m*/ * @return a List with data of the device * @throws KnotException KnotException */ public <T extends AbstractThingData> List<T> getDataList(String device, final KnotList<T> type, KnotQueryData knotQueryData) throws InvalidDeviceOwnerStateException, KnotException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } final String endPoint = mEndPoint + DATA_PATH + device + getDataParameter(knotQueryData); Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); JsonArray jsonArray = jsonElement.getAsJsonObject().getAsJsonArray(JSON_DATA); if (jsonArray == null || jsonArray.size() == 0) { return mGson.fromJson(EMPTY_ARRAY, type); } return mGson.fromJson(jsonArray.toString(), type); } catch (Exception e) { throw new KnotException(e); } }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
/** * Send a message in Meshblu instance/*w w w. jav a2 s . com*/ * * @param message model sample to create a new message. Basically this message model * contains attributes that will be send into Meshblu. * @return New message with meshblu content. * @throws KnotException KnotException * @see AbstractThingMessage */ public <T extends AbstractThingMessage> T sendMessage(T message) throws KnotException, InvalidDeviceOwnerStateException { // Check if the current state of device owner is valid if (!isValidDeviceOwner()) { throw new InvalidDeviceOwnerStateException("The device owner is invalid or null"); } final String endPoint = mEndPoint + MESSAGE; String json = mGson.toJson(message); RequestBody body = createRequestBodyWith(json); Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(), this.abstractDeviceOwner.getToken(), endPoint).post(body).build(); try { Response response = mHttpClient.newCall(request).execute(); JsonElement jsonElement = new JsonParser().parse(response.body().string()); return (T) mGson.fromJson(jsonElement.toString(), message.getClass()); } catch (Exception e) { throw new KnotException(e); } }
From source file:butter.droid.base.providers.media.AnimeProvider.java
License:Open Source License
/** * Fetch the list of movies from Haruhichan * * @param currentList Current shown list to be extended * @param requestBuilder Request to be executed * @param callback Network callback * @return Call// w w w . ja va 2 s .co m */ private Call fetchList(final ArrayList<Media> currentList, final Request.Builder requestBuilder, final Filters filters, final Callback callback) { return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { String url = requestBuilder.build().urlString(); if (CURRENT_API >= API_URLS.length - 1) { callback.onFailure(e); } else { if (url.contains(API_URLS[CURRENT_API])) { url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]); CURRENT_API++; } else { url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]); } requestBuilder.url(url); fetchList(currentList, requestBuilder, filters, callback); } } @Override public void onResponse(Response response) throws IOException { try { if (response.isSuccessful()) { String responseStr = response.body().string(); ArrayList<LinkedTreeMap<String, Object>> list = null; if (responseStr.isEmpty()) { list = new ArrayList<>(); } else { list = (ArrayList<LinkedTreeMap<String, Object>>) mGson.fromJson(responseStr, ArrayList.class); } AnimeResponse result = new AnimeResponse(list); if (list == null) { callback.onFailure(new NetworkErrorException("Empty response")); } else { ArrayList<Media> formattedData = result.formatListForPopcorn(currentList); callback.onSuccess(filters, formattedData, list.size() > 0); return; } } } catch (Exception e) { callback.onFailure(e); } callback.onFailure(new NetworkErrorException("Couldn't connect to AnimeAPI")); } }); }
From source file:butter.droid.base.providers.media.AnimeProvider.java
License:Open Source License
@Override public Call getDetail(ArrayList<Media> currentList, Integer index, final Callback callback) { Request.Builder requestBuilder = new Request.Builder(); String url = API_URLS[CURRENT_API] + "anime/" + currentList.get(index).videoId; requestBuilder.url(url);/* w w w . j a v a2 s .c o m*/ requestBuilder.tag(MEDIA_CALL); Timber.d("AnimeProvider", "Making request to: " + url); return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { callback.onFailure(e); } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String responseStr = response.body().string(); LinkedTreeMap<String, Object> map = mGson.fromJson(responseStr, LinkedTreeMap.class); AnimeResponse result = new AnimeResponse(map); if (map == null) { callback.onFailure(new NetworkErrorException("Empty response")); } else { ArrayList<Media> formattedData = result.formatDetailForPopcorn(); if (formattedData.size() > 0) { callback.onSuccess(null, formattedData, true); return; } callback.onFailure(new IllegalStateException("Empty list")); return; } } callback.onFailure(new NetworkErrorException("Couldn't connect to AnimeAPI")); } }); }
From source file:butter.droid.base.providers.media.MoviesProvider.java
License:Open Source License
/** * Fetch the list of movies from API//from ww w . j a va 2 s . c o m * * @param currentList Current shown list to be extended * @param requestBuilder Request to be executed * @param callback Network callback * @return Call */ private Call fetchList(final ArrayList<Media> currentList, final Request.Builder requestBuilder, final Filters filters, final Callback callback) { return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { String url = requestBuilder.build().urlString(); if (CURRENT_API >= API_URLS.length - 1) { callback.onFailure(e); } else { if (url.contains(API_URLS[CURRENT_API])) { url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]); CURRENT_API++; } else { url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]); } requestBuilder.url(url); fetchList(currentList, requestBuilder, filters, callback); } } @Override public void onResponse(Response response) throws IOException { try { if (response.isSuccessful()) { String responseStr = response.body().string(); ArrayList<LinkedTreeMap<String, Object>> list = null; if (responseStr.isEmpty()) { list = new ArrayList<>(); } else { list = (ArrayList<LinkedTreeMap<String, Object>>) mGson.fromJson(responseStr, ArrayList.class); } MovieResponse result = new MovieResponse(list); if (list == null) { callback.onFailure(new NetworkErrorException("Empty response")); } else { ArrayList<Media> formattedData = result.formatListForPopcorn(currentList); callback.onSuccess(filters, formattedData, list.size() > 0); return; } } } catch (Exception e) { callback.onFailure(e); } callback.onFailure(new NetworkErrorException("Couldn't connect to MovieAPI")); } }); }