Search in sources :

Example 21 with AjaxCallback

use of com.androidquery.callback.AjaxCallback in project howabout-android by recomio.

the class GroovesharkStreamUrlGetter method parseGroovesharkMainPage.

protected void parseGroovesharkMainPage(String body) throws JSONException, UnsupportedEncodingException {
    Pattern sessionIdPattern = Pattern.compile("(?:\"sessionID\":)\"[\\w]+");
    Matcher sessionIdMatcher = sessionIdPattern.matcher(body);
    while (sessionIdMatcher.find()) {
        sessionId = sessionIdMatcher.group().substring(13);
    }
    Pattern countryPattern = Pattern.compile("(?:\"country\":)\\{(.)*?\\}");
    Matcher countryMatcher = countryPattern.matcher(body);
    while (countryMatcher.find()) {
        country = countryMatcher.group().substring(10);
    }
    String secretKey = bytesToHex(md5.digest(sessionId.getBytes()));
    JSONObject headerObject = new JSONObject();
    headerObject.put("client", "mobileshark");
    headerObject.put("clientRevision", "20120830");
    headerObject.put("privacy", "0");
    headerObject.put("country", country);
    headerObject.put("uuid", uuid);
    headerObject.put("session", sessionId);
    JSONObject parametersObject = new JSONObject();
    parametersObject.put("secretKey", secretKey);
    JSONObject bodyObject = new JSONObject();
    bodyObject.put("header", headerObject);
    bodyObject.put("method", "getCommunicationToken");
    bodyObject.put("parameters", parametersObject);
    String groovesharkGetCommunicationTokenUrl = "https://html5.grooveshark.com/more.php?getCommunicationToken";
    AjaxCallback<JSONObject> groovesharkGetCommunicationTokenAjaxCallback = new AjaxCallback<JSONObject>();
    groovesharkGetCommunicationTokenAjaxCallback.url(groovesharkGetCommunicationTokenUrl).type(JSONObject.class).handler(this, "groovesharkGetCommunicationTokenCallback");
    HttpEntity bodyEntity = new StringEntity(bodyObject.toString());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(AQuery.POST_ENTITY, bodyEntity);
    groovesharkGetCommunicationTokenAjaxCallback.params(params);
    groovesharkGetCommunicationTokenAjaxCallback.header("Content-Type", "application/json");
    groovesharkGetCommunicationTokenAjaxCallback.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    groovesharkGetCommunicationTokenAjaxCallback.header("Accept-Charset", "UTF-8,*;q=0.5");
    groovesharkGetCommunicationTokenAjaxCallback.header("Accept-Language", "en-US,en;q=0.8");
    groovesharkGetCommunicationTokenAjaxCallback.header("Cache-Control", "max-age=0");
    groovesharkGetCommunicationTokenAjaxCallback.header("Connection", "keep-alive");
    groovesharkGetCommunicationTokenAjaxCallback.header("Host", "html5.grooveshark.com");
    groovesharkGetCommunicationTokenAjaxCallback.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
    aq.ajax(groovesharkGetCommunicationTokenAjaxCallback);
}
Also used : Pattern(java.util.regex.Pattern) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) HttpEntity(org.apache.http.HttpEntity) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) AjaxCallback(com.androidquery.callback.AjaxCallback)

Example 22 with AjaxCallback

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

the class AQueryAsyncTest method testAjaxTimeoutFiveSeconds.

public void testAjaxTimeoutFiveSeconds() {
    String url = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
    url = "http://deelay.me/10000/" + url;
    AjaxCallback.setTimeout(5000);
    AjaxCallback<File> cb = new AjaxCallback<File>();
    cb.url(url).type(File.class);
    long start = System.currentTimeMillis();
    aq.sync(cb);
    File file = cb.getResult();
    AjaxStatus status = cb.getStatus();
    long end = System.currentTimeMillis();
    long diff = end - start;
    AQUtility.debug("timeout", diff);
    assertTrue(diff < 10000);
    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 23 with AjaxCallback

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

the class AQueryAsyncTest method testAjaxLongBitmapURL.

public void testAjaxLongBitmapURL() {
    String dummy = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    String title = "Very long title " + dummy + dummy + dummy + dummy + dummy;
    AQUtility.debug("title len", title.length());
    String url = "https://chart.googleapis.com/chart?chid=1234&cht=lc&chtt=" + title + "&chs=300x200&chxt=x&chd=t:40,20,50,20,100";
    AjaxCallback<Bitmap> cb = new AjaxCallback<Bitmap>() {

        @Override
        public void callback(String url, Bitmap bm, AjaxStatus status) {
            done(url, bm, status);
        }
    };
    aq.ajax(url, Bitmap.class, 15 * 60 * 1000, cb);
    waitAsync(2000);
    assertNotNull(result);
    File cached = aq.getCachedFile(url);
    assertTrue(cached.exists());
    assertTrue(cached.length() > 100);
}
Also used : Bitmap(android.graphics.Bitmap) 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 24 with AjaxCallback

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

the class AQueryAsyncTest method testFileBatchDownloads.

public void testFileBatchDownloads() {
    String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
    List<String> urls = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        urls.add(url + "&test=" + i);
    }
    for (String u : urls) {
        AjaxCallback<File> cb = new AjaxCallback<File>();
        cb.type(File.class).fileCache(true).url(u);
        aq.sync(cb);
        File result = cb.getResult();
        AQUtility.debug("cached", result.getAbsoluteFile());
    }
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) AjaxCallback(com.androidquery.callback.AjaxCallback) AbstractAjaxCallback(com.androidquery.callback.AbstractAjaxCallback) BitmapAjaxCallback(com.androidquery.callback.BitmapAjaxCallback)

Example 25 with AjaxCallback

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

the class AQueryAsyncTest method testAjaxPostMultiWithProxy.

public void testAjaxPostMultiWithProxy() {
    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", data);
    params.put("data2", data2);
    AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject jo, AjaxStatus status) {
            AQUtility.debug(status.getCode(), status.getError());
            AQueryAsyncTest.this.result = jo;
        }
    };
    cb.proxy("192.168.0.102", 3128);
    aq.ajax(url, params, JSONObject.class, cb);
    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) 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