Search in sources :

Example 6 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project androidquery by androidquery.

the class AQueryAsyncTest method testWaitBlockInputStream.

public void testWaitBlockInputStream() {
    String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
    AjaxCallback<InputStream> cb = new AjaxCallback<InputStream>();
    cb.url(url).type(InputStream.class);
    aq.sync(cb);
    String u = cb.getUrl();
    InputStream is = cb.getResult();
    AjaxStatus status = cb.getStatus();
    byte[] data = AQUtility.toBytes(is);
    JSONObject jo = null;
    String str = null;
    try {
        str = new String(data, "UTF-8");
        jo = (JSONObject) new JSONTokener(str).nextValue();
    } catch (Exception e) {
        AQUtility.debug(e);
        AQUtility.debug(str);
    }
    assertNotNull(jo);
    assertNotNull(jo.opt("responseData"));
    checkStatus(status);
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException)

Example 7 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project androidquery by androidquery.

the class AQueryAsyncTest method testAjaxDelete.

public void testAjaxDelete() {
    String url = "http://www.androidquery.com/p/doNothing";
    AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject jo, AjaxStatus status) {
            done(url, jo, status);
        }
    };
    //aq.ajax(cb);
    aq.delete(url, JSONObject.class, cb);
    waitAsync();
    JSONObject jo = (JSONObject) result;
    AQUtility.debug(jo);
    assertNotNull(jo);
    assertEquals("DELETE", jo.optString("method"));
}
Also used : JSONObject(org.json.JSONObject) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 8 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project androidquery by androidquery.

the class AQueryAsyncTest method testAjaxTimeout.

public void testAjaxTimeout() {
    String url = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
    AjaxCallback<File> cb = new AjaxCallback<File>();
    cb.url(url).type(File.class).timeout(1);
    aq.sync(cb);
    File file = cb.getResult();
    AjaxStatus status = cb.getStatus();
    assertNull(file);
    assertTrue(status.getCode() == AjaxStatus.NETWORK_ERROR);
}
Also used : File(java.io.File) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 9 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project androidquery by androidquery.

the class AQueryAsyncTest method testAjaxSimulateError.

public void testAjaxSimulateError() {
    AjaxCallback.setSimulateError(true);
    String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
    AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject jo, AjaxStatus status) {
            done(url, jo, status);
        }
    };
    aq.ajax(url, JSONObject.class, cb);
    waitAsync();
    JSONObject jo = (JSONObject) result;
    assertNull(jo);
    assertNotNull(status);
    assertEquals(AjaxStatus.NETWORK_ERROR, status.getCode());
}
Also used : JSONObject(org.json.JSONObject) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus)

Example 10 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project androidquery by androidquery.

the class AQueryAsyncTest method testAjaxCache.

//Test: public <K> T ajax(String url, Class<K> type, long expire, AjaxCallback<K> callback)
public void testAjaxCache() {
    AQUtility.cleanCache(AQUtility.getCacheDir(getActivity()), 0, 0);
    String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
    File file = aq.getCachedFile(url);
    assertNull(file);
    AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject jo, AjaxStatus status) {
            done(url, jo, status);
        }
    };
    aq.ajax(url, JSONObject.class, 15 * 60 * 1000, cb);
    waitAsync(2000);
    JSONObject jo = (JSONObject) result;
    assertNotNull(jo);
    assertNotNull(jo.opt("responseData"));
    File cached = aq.getCachedFile(url);
    assertTrue(cached.exists());
    assertTrue(cached.length() > 100);
}
Also used : JSONObject(org.json.JSONObject) File(java.io.File) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback) AjaxStatus(com.androidquery.callback.AjaxStatus)

Aggregations

AjaxCallback (com.androidquery.callback.AjaxCallback)37 BitmapAjaxCallback (com.androidquery.callback.BitmapAjaxCallback)33 AbstractAjaxCallback (com.androidquery.callback.AbstractAjaxCallback)31 AjaxStatus (com.androidquery.callback.AjaxStatus)28 JSONObject (org.json.JSONObject)23 File (java.io.File)12 HashMap (java.util.HashMap)5 Bitmap (android.graphics.Bitmap)3 StringEntity (org.apache.http.entity.StringEntity)3 XmlDom (com.androidquery.util.XmlDom)2 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 HttpEntity (org.apache.http.HttpEntity)2 Activity (android.app.Activity)1 AQuery (com.androidquery.AQuery)1 GoogleHandle (com.androidquery.auth.GoogleHandle)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1