public static StreamConnection getConnectionForRequest(String url) {
    final ConnectionFactory connectionFactory = new ConnectionFactory();

    // Remove any transports that are not currently available.
    if (isTransportManaged == false) {
      for (int i = 0; i < availableTransportTypes.length; i++) {
        int transport = availableTransportTypes[i];
        if (!TransportInfo.isTransportTypeAvailable(transport)
            || !TransportInfo.hasSufficientCoverage(transport)) {
          Arrays.removeAt(availableTransportTypes, i);
        }
      }
      isTransportManaged = true;
    }

    connectionFactory.setPreferredTransportTypes(availableTransportTypes);

    final ConnectionDescriptor connectionDescriptor = connectionFactory.getConnection(url);
    StreamConnection connection = null;
    if (connectionDescriptor != null) {
      // connection suceeded
      int transportUsed = connectionDescriptor.getTransportDescriptor().getTransportType();
      System.out.println("Transport type used: " + Integer.toString(transportUsed));
      connection = (StreamConnection) connectionDescriptor.getConnection();
    }
    return connection;
  }
Пример #2
0
  public InputStream AsyncDownloadDoc(String url, String action) {
    InputStream is = null;

    try {
      ConnectionFactory cf = new ConnectionFactory();
      cf.setConnectionTimeout(3333);
      cf.setTimeLimit(33000);
      ConnectionDescriptor cd = cf.getConnection(url);
      HttpConnection httpConn;
      httpConn = (HttpConnection) cd.getConnection();
      int response = httpConn.getResponseCode();
      if (response == HttpConnection.HTTP_OK) {
        is = httpConn.openInputStream();
        // httpConn.close();
      }
      // httpConn.close();
      // DebugLabel("RES: "+response,tsmanager);

    } catch (IOException e1) {
      // DebugLabel(error_code("unknown"), j.gparent);
    } catch (Exception e) {
      // c.DebugLabel(c.error_code("unknown"), j.gparent);
    }

    return is;
  }
Пример #3
0
  public static EncodedImage getImage(String url, Manager parent) throws IOException {
    url += ";deviceside=true";
    HttpConnection connection = null;
    InputStream inputStream = null;
    try {
      ConnectionFactory cf = new ConnectionFactory();
      cf.setConnectionTimeout(2500);
      ConnectionDescriptor cd = cf.getConnection(url);
      HttpConnection httpConn;
      httpConn = (HttpConnection) cd.getConnection();
      inputStream = httpConn.openInputStream();
      StringBuffer rawResponse = new StringBuffer();
      byte[] responseData = new byte[10000];
      int length = 0;
      while (-1 != (length = inputStream.read(responseData))) {
        rawResponse.append(new String(responseData, 0, length));
      }
      String result = rawResponse.toString();
      byte[] dataArray = result.getBytes();
      // Image.createImage(InputStream);
      httpConn.close();
      return EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);

    } catch (IOException e1) {
      //
    }

    return null;
  }
  public void run() {
    if (URL != null && URL.length() > 0) {
      ConnectionFactory f = new ConnectionFactory();
      ConnectionDescriptor descr = f.getConnection(URL);
      String targetURL;
      targetURL = descr.getUrl();
      HttpConnection httpConnection = null;
      DataOutputStream httpDataOutput = null;
      InputStream httpInput = null;
      int rc;

      try {
        httpConnection = (HttpConnection) Connector.open(targetURL);
        rc = httpConnection.getResponseCode();
        if (rc != HttpConnection.HTTP_OK) {
          throw new IOException("HTTP response code: " + rc);
        }
        httpInput = httpConnection.openInputStream();
        InputStream inp = httpInput;
        byte[] b = IOUtilities.streamToBytes(inp);
        final EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
        final Bitmap bmp = hai.getBitmap();
        System.out.println(bmp.toString());

        if (target != null) {
          UiApplication.getUiApplication()
              .invokeLater(
                  new Runnable() {

                    public void run() {
                      // TODO Auto-generated method stub
                      if (isDetailEvent == true) {
                        target.setBitmap(
                            DisplayHelper.CreateScaledCopyKeepAspectRatio(
                                bmp,
                                (int) (Display.getWidth() * 0.7),
                                (int) (Display.getHeight() * 0.3)));
                      } else {
                        target.setBitmap(
                            DisplayHelper.CreateScaledCopyKeepAspectRatio(
                                bmp, target.getBitmapWidth(), target.getBitmapHeight()));
                      }
                    }
                  });
        }

        if (localPath != null) {
          Utils.saveBitmap(localPath, bmp);
        }

        if (imageCache != null) {
          if (Utils.saveBitmap(
              ImageCacheModel.getImageCacheDirectory() + imageCache.getFileName(), bmp)) {
            //		    		   CacheUtils.getInstance().addImageCache(imageCache);
            System.out.println("image write success");
          }
        }

        if (callback != null) callback.onImageDownloaded(true, bmp);

      } catch (Exception ex) {
        System.out.println("URL Bitmap Error........" + ex.getMessage());
      } finally {
        try {
          if (httpInput != null) httpInput.close();
          if (httpDataOutput != null) httpDataOutput.close();
          if (httpConnection != null) httpConnection.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }