Search in sources :

Example 81 with JsonObject

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"));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) File(java.io.File) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 82 with JsonObject

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"));
}
Also used : JSONObject(org.json.JSONObject) BasicHandle(com.androidquery.auth.BasicHandle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 83 with JsonObject

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);
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 84 with JsonObject

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"));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 85 with JsonObject

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) {
            }
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException)

Aggregations

JsonObject (com.google.gson.JsonObject)1417 JsonElement (com.google.gson.JsonElement)389 JsonArray (com.google.gson.JsonArray)293 JsonParser (com.google.gson.JsonParser)285 JsonPrimitive (com.google.gson.JsonPrimitive)137 Gson (com.google.gson.Gson)91 Test (org.junit.Test)81 HashMap (java.util.HashMap)79 Map (java.util.Map)78 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)66 Test (org.testng.annotations.Test)61 InputStreamReader (java.io.InputStreamReader)38 JsonParseException (com.google.gson.JsonParseException)27 File (java.io.File)25 List (java.util.List)21 HttpResponse (org.apache.http.HttpResponse)21 JsonReader (com.google.gson.stream.JsonReader)19 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 InputStream (java.io.InputStream)19