Beispiel #1
0
 private Object request(
     String url, boolean post, Hashtable params, boolean basicAuth, boolean processOutput)
     throws Exception {
   HttpConnection conn = null;
   Writer writer = null;
   InputStream is = null;
   try {
     if (!post && (params != null)) {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(baos, this.encoding);
       this.encodeParams(params, osw);
       osw.flush();
       osw.close();
       osw = null;
       url += "?" + new String(baos.toByteArray(), this.encoding);
       baos = null;
     }
     conn = (HttpConnection) Connector.open(url);
     conn.setRequestMethod(post ? HttpConnection.POST : HttpConnection.GET);
     if (basicAuth) {
       if (!this.areCredentialsSet()) throw new Exception("Credentials are not set");
       String token = base64Encode((this.user + ":" + this.pass).getBytes(this.encoding));
       conn.setRequestProperty("Authorization", "Basic " + token);
     }
     if (post && (params != null)) {
       OutputStream os = conn.openOutputStream();
       writer = new OutputStreamWriter(os, this.encoding);
       this.encodeParams(params, writer);
       writer.flush();
       writer.close();
       os = null;
       writer = null;
     }
     int code = conn.getResponseCode();
     if ((code != 200) && (code != 302))
       throw new Exception("Unexpected response code " + code + ": " + conn.getResponseMessage());
     is = conn.openInputStream();
     if (processOutput) {
       synchronized (this.json) {
         return this.json.parse(is);
       }
     } else {
       this.pump(is, System.out, 1024);
       return null;
     }
   } finally {
     if (writer != null) writer.close();
     if (is != null) is.close();
     if (conn != null) conn.close();
   }
 }
  /** Implementation of Thread. */
  public void run() {
    StreamConnection connection = null;

    try {
      _screen.updateDisplay("Opening Connection...");
      String url =
          "socket://"
              + _screen.getHostFieldText()
              + ":44444;interface=wifi"
              + (_screen.isDirectTCP() ? ";deviceside=true" : "");
      connection = (StreamConnection) Connector.open(url);
      _screen.updateDisplay("Connection with " + _screen.getHostFieldText() + " established.");

      _in = connection.openInputStream();
      _out = new OutputStreamWriter(connection.openOutputStream());

      // Send the HELLO string.
      // send("Hello from BlackBerry 9860.");
      // send("Hello from BlackBerry 9860.");
      send(AzrRP_Screen.getMsg());

      // Execute further data exchange here...

      // send("Bye");

      _screen.updateDisplay("Done!");
    } catch (IOException e) {
      System.err.println(e.toString());
    } finally {
      _screen.setThreadRunning(false);

      try {
        _in.close();
      } catch (IOException ioe) {
      }
      try {
        _out.close();
        AzrRP_Screen.setMsg(null);

      } catch (IOException ioe) {
      }
      try {
        connection.close();

      } catch (IOException ioe) {
      }
    }
  }