Android Open Source - kirin-for-android Kirin X H R Extension Impl






From Project

Back to project page kirin-for-android.

License

The source code is released under:

Apache License

If you think the Android project kirin-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.futureplatforms.kirin.extensions.networking;
//from w w  w.  ja  v  a 2  s .  c  o  m
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.futureplatforms.kirin.C;
import com.futureplatforms.kirin.extensions.KirinExtensionAdapter;
import com.futureplatforms.kirin.generated.xhr.KirinXHRExtension;
import com.futureplatforms.kirin.generated.xhr.KirinXHRProgressEvent;
import com.futureplatforms.kirin.generated.xhr.KirinXHRResponse;
import com.futureplatforms.kirin.generated.xhr.KirinXHRequest;

public class KirinXHRExtensionImpl extends KirinExtensionAdapter implements
    KirinXHRExtension {

  public KirinXHRExtensionImpl(Context context) {
    super(context, "KirinXMLHTTPRequest");
  }

  @Override
  public void open(KirinXHRequest xhrObject, Object data) {
    String urlString = xhrObject.getUrl();

    URL url;
    URLConnection connection = null;
    try {
      url = new URL(urlString);
      connection = url.openConnection();
    } catch (MalformedURLException e) {
      Log.e(C.TAG, "Error constructing url to " + urlString, e);
      xhrObject._doOnInitialisationError(e.getLocalizedMessage());
    } catch (IOException e) {
      Log.e(C.TAG, "Error connecting to " + urlString, e);
      xhrObject._doOnInitialisationError(e.getLocalizedMessage());
    }
    if (connection == null) {
      return;
    }
    
    
    
    JSONObject headers = xhrObject.getRequestHeaders();
    for (@SuppressWarnings("unchecked")
    Iterator<String> iterator = headers.keys(); iterator.hasNext();) {
      String header = iterator.next();
      String value = headers.optString(header);
      connection.addRequestProperty(header, value);
    }
    
    connection.setReadTimeout((int) xhrObject.getTimeout());
    
    if (xhrObject.getResponseType() != null) {
      // TODO implement getResponseType() - not sure how to deal with this.
      // we can be "text", "json", "document", "arraybuffer", "blob"
    }
    
    try {
      connection.connect();
      if (data != null) {
        // TODO send the data.
        // this may include file upload data.
      }
      
      JSONObject responseHeadersJson = new JSONObject();
      if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        
        
        Map<String, List<String>> responseHeaders = httpConnection.getHeaderFields();
        
        for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
          try {
            responseHeadersJson.putOpt(entry.getKey(), new JSONArray(entry.getValue()));
          } catch (JSONException e) {
            Log.e(C.TAG, "Problem with response header " + entry.getKey(), e);
          }
        }
      }

      xhrObject._doOnConnect(responseHeadersJson, newProgressEvent(0, 0));
      
      
      // Now we should download whatever we're given.
      
      // TODO download all the stuffs from that we need.
      
      BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
      
      
      
      
      Object downloaded = connection.getContent();

      if (downloaded != null) {
        downloaded = downloaded.toString();
      } else {
        downloaded = "";
      }
      
      // Now we've downloaded, we should finish up.
      KirinXHRResponse response = newResponse();
    
      if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        response.setStatus(httpConnection.getResponseCode());
        response.setStatusText(httpConnection.getResponseMessage());
      } else {
        response.setStatus(HttpURLConnection.HTTP_OK);
        response.setStatusText("OK");
      }
      
      
      xhrObject._doOnRequestComplete(response.getStatus(), response, newProgressEvent(0, 0));
      
    } catch (IOException e) {
      // was this a timeout
      
      // was this a connectivity problem?
    }    
  }

  private KirinXHRResponse newResponse() {
    return this.mKirinHelper.javascriptProxyForValueObject(new JSONObject(), KirinXHRResponse.class);
  }
  
  private KirinXHRProgressEvent newProgressEvent(int loaded, int total) {
    KirinXHRProgressEvent event = this.mKirinHelper.javascriptProxyForValueObject(new JSONObject(), KirinXHRProgressEvent.class);
    event.setLoaded(loaded);
    event.setTotal(total);
    return event;
  }

  @Override
  public void abort(String xhrId) {
    
  }

}




Java Source Code List

com.futureplatforms.kirin.C.java
com.futureplatforms.kirin.Kirin.java
com.futureplatforms.kirin.TestingApplication.java
com.futureplatforms.kirin.activities.KirinActivity.java
com.futureplatforms.kirin.activities.KirinListActivity.java
com.futureplatforms.kirin.application.IKirinApplication.java
com.futureplatforms.kirin.application.KirinApplication.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnNonDefaultThread.java
com.futureplatforms.kirin.extensions.IKirinExtensionOnUiThread.java
com.futureplatforms.kirin.extensions.IKirinExtension.java
com.futureplatforms.kirin.extensions.IProguardImmunity.java
com.futureplatforms.kirin.extensions.KirinExtensionAdapter.java
com.futureplatforms.kirin.extensions.KirinExtensions.java
com.futureplatforms.kirin.extensions.databases.DBStatement.java
com.futureplatforms.kirin.extensions.databases.DBTransaction.java
com.futureplatforms.kirin.extensions.databases.DatabasesBackend.java
com.futureplatforms.kirin.extensions.databases.IDatabasesBackend.java
com.futureplatforms.kirin.extensions.fs.KirinFilesystemExtensionImpl.java
com.futureplatforms.kirin.extensions.localnotifications.ILocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationAlarmReceiver.java
com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java
com.futureplatforms.kirin.extensions.networking.INetworkingBackend.java
com.futureplatforms.kirin.extensions.networking.KirinXHRExtensionImpl.java
com.futureplatforms.kirin.extensions.networking.NetworkingBackend.java
com.futureplatforms.kirin.extensions.settings.ISettingsBackend.java
com.futureplatforms.kirin.extensions.settings.PreferencesBackendImpl.java
com.futureplatforms.kirin.extensions.settings.SettingsBackend.java
com.futureplatforms.kirin.generated.fs.KirinCallback.java
com.futureplatforms.kirin.generated.fs.KirinFile.java
com.futureplatforms.kirin.generated.fs.KirinFilesystemExtension.java
com.futureplatforms.kirin.generated.fs.KirinFilesystem.java
com.futureplatforms.kirin.generated.fs.KirinFoundFile.java
com.futureplatforms.kirin.generated.fs.KirinOptionalCallback.java
com.futureplatforms.kirin.generated.location.KirinLocationBackend.java
com.futureplatforms.kirin.generated.location.KirinLocationData.java
com.futureplatforms.kirin.generated.location.KirinLocationListener.java
com.futureplatforms.kirin.generated.location.KirinLocationPermissions.java
com.futureplatforms.kirin.generated.location.KirinLocation.java
com.futureplatforms.kirin.generated.preferences.KirinPreferenceListener.java
com.futureplatforms.kirin.generated.preferences.KirinPreferencesBackend.java
com.futureplatforms.kirin.generated.preferences.KirinPreferences.java
com.futureplatforms.kirin.generated.xhr.KirinXHRExtension.java
com.futureplatforms.kirin.generated.xhr.KirinXHRProgressEvent.java
com.futureplatforms.kirin.generated.xhr.KirinXHRResponse.java
com.futureplatforms.kirin.generated.xhr.KirinXHRequest.java
com.futureplatforms.kirin.generated.xhr.KirinXMLHTTPRequest.java
com.futureplatforms.kirin.helpers.IKirinExtensionHelper.java
com.futureplatforms.kirin.helpers.IKirinHelper.java
com.futureplatforms.kirin.helpers.KirinApplicationHelper.java
com.futureplatforms.kirin.helpers.KirinExtensionHelper.java
com.futureplatforms.kirin.helpers.KirinHelper.java
com.futureplatforms.kirin.helpers.KirinScreenHelper.java
com.futureplatforms.kirin.helpers.KirinUiFragmentHelper.java
com.futureplatforms.kirin.internal.JSONUtils.java
com.futureplatforms.kirin.internal.KirinPaths.java
com.futureplatforms.kirin.internal.attic.IOUtils.java
com.futureplatforms.kirin.internal.attic.KirinDropbox.java
com.futureplatforms.kirin.internal.attic.ProxyGenerator.java
com.futureplatforms.kirin.internal.attic.SDCardFileUtils.java
com.futureplatforms.kirin.internal.core.AbstractObjectHolder.java
com.futureplatforms.kirin.internal.core.DefaultObjectHandler.java
com.futureplatforms.kirin.internal.core.IJsContext.java
com.futureplatforms.kirin.internal.core.IKirinState.java
com.futureplatforms.kirin.internal.core.INativeContext.java
com.futureplatforms.kirin.internal.core.IObjectHolder.java
com.futureplatforms.kirin.internal.core.JsCommands.java
com.futureplatforms.kirin.internal.core.KirinAppState.java
com.futureplatforms.kirin.internal.core.KirinWebViewHolder.java
com.futureplatforms.kirin.internal.core.NativeContext.java
com.futureplatforms.kirin.internal.core.UiObjectHolder.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer4.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer5.java
com.futureplatforms.kirin.internal.fragmentation.CursorCoercer.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient7.java
com.futureplatforms.kirin.internal.fragmentation.WebChromeClient8.java
com.futureplatforms.kirin.state.IKirinDropbox.java
com.futureplatforms.kirin.state.IKirinFileSystem.java
com.futureplatforms.kirin.ui.DebugMenuActions.java
com.futureplatforms.kirin.ui.JSListAdapter.java
com.futureplatforms.kirin.ui.JSOnClickListener.java
com.futureplatforms.kirin.ui.KirinRowRenderer.java