public void sendError(final String error, final String syncId) {
   Log.i(LCAT, "Sending error " + error);
   final TitaniumWebView webView = softWebView.get();
   if (webView != null) {
     final String cb = onErrorCallback;
     if (cb != null) {
       webView.evalJS(cb, "\"" + error + "\"", syncId);
     }
   }
 }
 public void setReadyState(final int readyState, final String syncId) {
   Log.d(LCAT, "Setting ready state to " + readyState);
   this.readyState = readyState;
   final TitaniumWebView webView = softWebView.get();
   if (webView != null) {
     final String cb = getOnReadyStateChangeCallback();
     if (cb != null) {
       webView.evalJS(cb, null, syncId);
     }
     if (readyState == ITitaniumHttpClient.READY_STATE_COMPLETE) {
       // Fire onload callback
       final String cbOnLoad = onLoadCallback;
       if (cbOnLoad != null) {
         webView.evalJS(cbOnLoad, null, syncId);
       }
     }
   }
 }
  /* (non-Javadoc)
   * @see org.appcelerator.titanium.module.ITitaniumHttpClient#open(java.lang.String, java.lang.String)
   */
  public void open(String method, String url) throws MethodNotSupportedException {
    if (DBG) {
      Log.d(LCAT, "open request method=" + method + " url=" + url);
    }
    TitaniumWebView wv = softWebView.get();
    if (wv != null) {
      me.syncId = wv.registerLock();
    }

    request = new DefaultHttpRequestFactory().newHttpRequest(method, url);
    Uri uri = Uri.parse(url);
    host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    if (uri.getUserInfo() != null) {
      credentials = new UsernamePasswordCredentials(uri.getUserInfo());
    }
    setReadyState(READY_STATE_LOADING, syncId);
    setRequestHeader("User-Agent", userAgent);
    setRequestHeader("X-Requested-With", "XMLHttpRequest");
  }
  public void invokeErrorListeners(String eventName, String data) {
    if (eventListeners != null) {
      checkSupportsEvent(eventName); // Throws exception on failure

      final HashMap<Integer, TitaniumJSEvent> listeners = eventListeners.get(eventName);

      if (listeners != null) {
        synchronized (listeners) {
          final TitaniumWebView webView = softWebView.get();
          if (webView == null) {
            throw new IllegalStateException("WebView is null");
          }
          for (TitaniumJSEvent event : listeners.values()) {
            String listener = event.getErrorListener();
            if (listener != null) {
              webView.evalJS(listener, data);
            }
          }
        }
      }
    } else {
      Log.w(LCAT, "eventListeners is null, not invoke success event for eventName: " + eventName);
    }
  }
    public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
      String clientResponse = null;

      if (client != null) {
        TitaniumHttpClient c = client.get();
        if (c != null) {
          c.response = response;
          c.setReadyState(READY_STATE_LOADED, syncId);
          c.setStatus(response.getStatusLine().getStatusCode());
          c.setStatusText(response.getStatusLine().getReasonPhrase());
          c.setReadyState(READY_STATE_INTERACTIVE, syncId);
        }

        if (DBG) {
          try {
            Log.w(LCAT, "Entity Type: " + response.getEntity().getClass());
            Log.w(LCAT, "Entity Content Type: " + response.getEntity().getContentType().getValue());
            Log.w(LCAT, "Entity isChunked: " + response.getEntity().isChunked());
            Log.w(LCAT, "Entity isStreaming: " + response.getEntity().isStreaming());
          } catch (Throwable t) {
            // Ignore
          }
        }

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        entity = response.getEntity();

        if (c.onDataStreamCallback != null) {
          is = entity.getContent();

          if (is != null) {
            final String cb = c.onDataStreamCallback;
            final TitaniumWebView webView = softWebView.get();

            long contentLength = entity.getContentLength();
            if (DBG) {
              Log.d(LCAT, "Content length: " + contentLength);
            }
            int count = 0;
            int totalSize = 0;
            byte[] buf = new byte[4096];
            if (DBG) {
              Log.d(LCAT, "Available: " + is.available());
            }
            if (aborted) {
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              while ((count = is.read(buf)) != -1) {
                totalSize += count;
                if (webView != null) {
                  TitaniumModuleManager tmm = weakTmm.get();
                  if (tmm != null) {
                    try {
                      JSONObject o = new JSONObject();
                      o.put("totalCount", contentLength);
                      o.put("totalSize", totalSize);
                      o.put("size", count);
                      byte[] newbuf = new byte[count];
                      for (int i = 0; i < count; i++) {
                        newbuf[i] = buf[i];
                      }
                      TitaniumMemoryBlob blob = new TitaniumMemoryBlob(newbuf);
                      int key = tmm.cacheObject(blob);
                      o.put("key", key);
                      webView.evalJS(cb, o.toString(), syncId);
                    } catch (JSONException e) {
                      Log.e(LCAT, "Unable to send ondatastream event: ", e);
                    }
                  }
                }
                if (!entity.isStreaming()) {
                  break;
                }
              }
              if (entity != null) {
                entity.consumeContent();
              }
            }
          }
        } else {
          setResponseData(entity);
        }
      }
      return clientResponse;
    }