use of com.google.gson.JsonObject in project androidquery by androidquery.
the class AQueryAsyncTest method testAjaxPostMultiFile.
public void testAjaxPostMultiFile() throws IOException {
String url = "http://www.androidquery.com/p/multipart";
Map<String, Object> params = new HashMap<String, Object>();
File tempFile1 = AQUtility.getCacheFile(AQUtility.getCacheDir(getActivity()), "pre1");
File tempFile2 = AQUtility.getCacheFile(AQUtility.getCacheDir(getActivity()), "pre2");
byte[] data1 = new byte[1234];
byte[] data2 = new byte[2345];
AQUtility.write(tempFile1, data1);
AQUtility.write(tempFile2, data2);
params.put("data", tempFile1);
params.put("data2", tempFile2);
aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject jo, AjaxStatus status) {
AQUtility.debug(status.getCode(), status.getError());
AQueryAsyncTest.this.result = jo;
}
});
waitAsync();
JSONObject jo = (JSONObject) result;
AQUtility.debug(jo);
assertNotNull(jo);
assertEquals(1234, jo.optInt("data"));
assertEquals(2345, jo.optInt("data2"));
}
use of com.google.gson.JsonObject in project androidquery by androidquery.
the class AQueryAsyncTest method testAjaxPostMultiAuth.
public void testAjaxPostMultiAuth() {
String url = "http://www.androidquery.com/p/multipart";
BasicHandle handle = new BasicHandle("username", "1234");
Map<String, Object> params = new HashMap<String, Object>();
byte[] data = new byte[1234];
byte[] data2 = new byte[2345];
params.put("data", data);
params.put("data2", data2);
aq.auth(handle).ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject jo, AjaxStatus status) {
AQUtility.debug(status.getCode(), status.getError());
AQueryAsyncTest.this.result = jo;
}
});
waitAsync();
JSONObject jo = (JSONObject) result;
AQUtility.debug(jo);
assertNotNull(jo);
assertEquals(1234, jo.optInt("data"));
assertEquals(2345, jo.optInt("data2"));
JSONObject headers = jo.optJSONObject("headers");
assertNotNull(headers.optString("Authorization"));
}
use of com.google.gson.JsonObject in project androidquery by androidquery.
the class AQueryAsyncTest method testAjaxPostMultiError.
public void testAjaxPostMultiError() {
String url = "http://www.androidquery.com/p/multipart2";
Map<String, Object> params = new HashMap<String, Object>();
byte[] data = new byte[1234];
byte[] data2 = new byte[2345];
params.put("data", data);
params.put("data2", data2);
aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject jo, AjaxStatus status) {
AQUtility.debug(status.getCode(), status.getError());
AQueryAsyncTest.this.result = jo;
AQueryAsyncTest.this.status = status;
}
});
waitAsync();
JSONObject jo = (JSONObject) result;
AQUtility.debug("error code", status.getCode());
assertNull(jo);
assertEquals(404, status.getCode());
String error = status.getError();
assertNotNull(error);
}
use of com.google.gson.JsonObject in project androidquery by androidquery.
the class AQueryAsyncTest method testAjaxPostMultiInputStream.
public void testAjaxPostMultiInputStream() {
String url = "http://www.androidquery.com/p/multipart";
Map<String, Object> params = new HashMap<String, Object>();
byte[] data = new byte[1234];
byte[] data2 = new byte[2345];
params.put("data", new ByteArrayInputStream(data));
params.put("data2", new ByteArrayInputStream(data2));
aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject jo, AjaxStatus status) {
AQUtility.debug(status.getCode(), status.getError());
AQueryAsyncTest.this.result = jo;
}
});
waitAsync();
JSONObject jo = (JSONObject) result;
AQUtility.debug(jo);
assertNotNull(jo);
assertEquals(1234, jo.optInt("data"));
assertEquals(2345, jo.optInt("data2"));
}
use of com.google.gson.JsonObject in project openhab1-addons by openhab.
the class MystromClient method ChangeState.
/*
* (non-Javadoc)
*
* @see
* org.openhab.binding.mystromecopower.internal.api.IMystromClient#ChangeState
* (java.lang.String, java.lang.Boolean)
*/
@Override
public Boolean ChangeState(String deviceId, Boolean newStateIsOn) {
Reader reader = null;
logger.debug("Change state for device id '{}', new state is on: '{}'", deviceId, newStateIsOn);
try {
String url = API_URL + "device/switch" + "?authToken=" + this.authToken + "&id=" + deviceId + "&on=" + newStateIsOn.toString();
HttpURLConnection httpURLConnection;
httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
reader = new InputStreamReader(inputStream, "UTF-8");
JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
String status = jsonObject.get("status").getAsString();
if (!status.equals("ok")) {
String error = jsonObject.get("error").getAsString();
logger.error("Unable to switch state for device '{}' error '{}'", deviceId, error);
return false;
}
String newState = jsonObject.get("state").getAsString();
logger.debug("Switch state for device '{}' successful, state is '{}'", deviceId, newState);
return true;
} catch (Exception ex) {
logger.error("Error set state: '{}'", ex.toString());
return false;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}
Aggregations