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);
       }
     }
   }
 }
 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 invokeSuccessListeners(TitaniumWebView webView, 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) {
          if (webView == null) {
            throw new IllegalStateException("webView is null");
          }
          for (TitaniumJSEvent event : listeners.values()) {
            String listener = event.getSuccessListener();
            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;
    }