3434import com .codename1 .ui .events .ActionEvent ;
3535import com .codename1 .ui .events .ActionListener ;
3636import com .codename1 .ui .util .EventDispatcher ;
37+ import com .codename1 .util .AsyncResource ;
3738import com .codename1 .util .Base64 ;
3839import com .codename1 .util .Callback ;
3940import com .codename1 .util .CallbackAdapter ;
@@ -2417,6 +2418,48 @@ public static Map<String, Object> fetchJSON(String url) throws IOException {
24172418 return result ;
24182419 }
24192420
2421+ /**
2422+ * Fetches JSON asynchronously.
2423+ * @param url The URL to fetch.
2424+ * @return AsyncResource that will resolve with either an exception or the parsed JSON data.
2425+ * @since 7.0
2426+ */
2427+ public static AsyncResource <Map <String , Object >> fetchJSONAsync (String url ) {
2428+ final AsyncResource <Map <String , Object >> out = new AsyncResource <Map <String , Object >>();
2429+ final ConnectionRequest cr = new ConnectionRequest ();
2430+ cr .setFailSilently (true );
2431+ cr .setPost (false );
2432+ cr .setUrl (url );
2433+ cr .addResponseListener (new ActionListener <NetworkEvent >() {
2434+ @ Override
2435+ public void actionPerformed (NetworkEvent evt ) {
2436+ if (out .isDone ()) {
2437+ return ;
2438+ }
2439+ if (cr .getResponseData () == null ) {
2440+ if (cr .failureException != null ) {
2441+ out .error (new IOException (cr .failureException .toString ()));
2442+ return ;
2443+ } else {
2444+ out .error (new IOException ("Server returned error code: " + cr .failureErrorCode ));
2445+ return ;
2446+ }
2447+ }
2448+ JSONParser jp = new JSONParser ();
2449+ Map <String ,Object > result = null ;
2450+ try {
2451+ result = jp .parseJSON (new InputStreamReader (new ByteArrayInputStream (cr .getResponseData ()), "UTF-8" ));
2452+ } catch (IOException ex ) {
2453+ out .error (ex );
2454+ return ;
2455+ }
2456+ out .complete (result );
2457+ }
2458+ });
2459+ NetworkManager .getInstance ().addToQueue (cr );
2460+ return out ;
2461+ }
2462+
24202463 /**
24212464 * Downloads an image to a specified storage file asynchronously and calls the onSuccessCallback with the resulting image.
24222465 * If useCache is true, then this will first try to load the image from Storage if it exists.
@@ -2431,6 +2474,44 @@ public void downloadImageToStorage(String storageFile, final SuccessCallback<Ima
24312474 downloadImage (onSuccess , onFail , useCache );
24322475 }
24332476
2477+ /**
2478+ * Downloads an image to a specified storage file asynchronously returning an AsyncResource that resolves to the resulting image..
2479+ * @param storageFile The storage file where the file should be saved.
2480+ * @return AsyncResource<Image> that will resolve to the loaded image.
2481+ * @since 7.0
2482+ */
2483+ public AsyncResource <Image > downloadImageToStorage (String storageFile ) {
2484+ return downloadImageToStorage (storageFile , true );
2485+ }
2486+
2487+ /**
2488+ * Downloads an image to a specified storage file asynchronously returning an AsyncResource that resolves to the resulting image..
2489+ * If useCache is true, then this will first try to load the image from Storage if it exists.
2490+ * @param storageFile The storage file where the file should be saved.
2491+ * @param useCache If true, then this will first check the storage to see if the image is already downloaded.
2492+ * @return AsyncResource<Image> that will resolve to the loaded image.
2493+ * @since 7.0
2494+ */
2495+ public AsyncResource <Image > downloadImageToStorage (String storageFile , boolean useCache ) {
2496+ final AsyncResource <Image > out = new AsyncResource <Image >();
2497+ downloadImageToStorage (storageFile , new SuccessCallback <Image >() {
2498+ @ Override
2499+ public void onSucess (Image value ) {
2500+ if (!out .isDone ()) {
2501+ out .complete (value );
2502+ }
2503+ }
2504+ }, new FailureCallback <Image >() {
2505+ @ Override
2506+ public void onError (Object sender , Throwable err , int errorCode , String errorMessage ) {
2507+ if (!out .isDone ()) {
2508+ out .error (err );
2509+ }
2510+ }
2511+ }, useCache );
2512+ return out ;
2513+ }
2514+
24342515 /**
24352516 * Downloads an image to a specified storage file asynchronously and calls the onSuccessCallback with the resulting image.
24362517 * If useCache is true, then this will first try to load the image from Storage if it exists.
@@ -2468,6 +2549,53 @@ public void downloadImageToStorage(String storageFile, SuccessCallback<Image> on
24682549 downloadImageToStorage (storageFile , onSuccess , onFail , true );
24692550 }
24702551
2552+ /**
2553+ * Downloads an image to a the file system asynchronously returning an AsyncResource object that resolves to the loaded image..
2554+ * If useCache is true, then this will first try to load the image from Storage if it exists.
2555+ *
2556+ * @param file The storage file where the file should be saved.
2557+ * @param useCache If true, then this will first check the storage to see if the image is already downloaded.
2558+ * @return AsyncResource resolving to the downloaded image.
2559+ * @since 7.0
2560+ */
2561+ public AsyncResource <Image > downloadImageToFileSystem (String file , boolean useCache ) {
2562+ final AsyncResource <Image > out = new AsyncResource <Image >();
2563+ downloadImageToFileSystem (file , new SuccessCallback <Image >() {
2564+ @ Override
2565+ public void onSucess (Image value ) {
2566+ if (out .isDone ()) {
2567+ return ;
2568+ }
2569+ out .complete (value );
2570+ }
2571+ }, new FailureCallback <Image >() {
2572+ @ Override
2573+ public void onError (Object sender , Throwable err , int errorCode , String errorMessage ) {
2574+ if (out .isDone ()) {
2575+ return ;
2576+ }
2577+ out .error (err );
2578+ }
2579+ }, useCache );
2580+ return out ;
2581+ }
2582+
2583+
2584+ /**
2585+ * Downloads an image to a the file system asynchronously returning an AsyncResource object that resolves to the loaded image..
2586+ * If useCache is true, then this will first try to load the image from Storage if it exists. This is a wrapper around {@link #downloadImageToFileSystem(java.lang.String, boolean) }
2587+ * with {@literal true} as the 2nd parameter.
2588+ *
2589+ * @param file The storage file where the file should be saved.
2590+
2591+ * @return AsyncResource resolving to the downloaded image.
2592+ * @since 7.0
2593+ */
2594+ public AsyncResource <Image > downloadImageToFileSystem (String file ) {
2595+ return downloadImageToFileSystem (file , true );
2596+ }
2597+
2598+
24712599 /**
24722600 * Downloads an image to a the file system asynchronously and calls the onSuccessCallback with the resulting image.
24732601 * If useCache is true, then this will first try to load the image from Storage if it exists.
0 commit comments