Ejemplo n.º 1
0
  public void call(String number) {
    Calendar calendar = Calendar.getInstance();
    Traces.addLine("call: " + calendar.get(Calendar.MINUTE) + " " + calendar.get(Calendar.SECOND));

    this.showCallScreen(number);

    try {
      this.platformRequest("tel:" + number);
    } catch (ConnectionNotFoundException ex) {
      Traces.addLine(ex.getMessage());
    }
  }
  public String ReadLocalFile() throws IOException {
    FileConnection fc = null;
    InputStream is = null;
    StringBuffer sb = new StringBuffer();

    try {
      // String local_code = ReadLocalCode("expe");
      String local_code = "201102";
      String url = sFileSchema + sFSRoot + sLocalExpePrefix + local_code + sLocalDataExt;
      fc = (FileConnection) Connector.open(url, Connector.READ);
      if (fc == null) {
        SetMessages("[!] ERROR: fc == null.");
        System.out.println(GetMessages());
        return null;
      }
      if (!fc.exists()) {
        SetMessages("[!] ERROR: !fc.exists().");
        System.out.println(GetMessages());
        return null;
      }

      is = fc.openInputStream();
      int ch;
      while ((ch = is.read()) != -1) {
        sb.append((char) ch);
      }

      // System.out.println(sb.toString());
    } catch (ConnectionNotFoundException cnfex) {
      SetMessages(cnfex.toString());
      cnfex.printStackTrace();
    } catch (IOException ioex) {
      SetMessages(ioex.toString());
      ioex.printStackTrace();
    } catch (Exception ex) {
      SetMessages(ex.toString());
      ex.printStackTrace();
    } finally {
      if (is != null) {
        is.close();
      }

      if (fc != null) {
        fc.close();
      }
    }

    return sb.toString();
  }
  public String ReadOutputFile() throws IOException {
    FileConnection fc = null;
    InputStream is = null;
    StringBuffer sb = new StringBuffer();

    try {
      String url = sFileSchema + sFSRoot + sLocalOutputPrefix + "201101" + sOutputExt;
      fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
      if (fc == null) {
        SetMessages("[!] ERROR: fc == null.");
        System.out.println(GetMessages());
        return null;
      }
      if (!fc.exists()) {
        // SetMessages("[!] ERROR: !fc.exists().");
        // System.out.println(GetMessages());
        fc.create();
        return "";
      }

      is = fc.openInputStream();
      int ch;
      while ((ch = is.read()) != -1) {
        sb.append((char) ch);
      }
    } catch (ConnectionNotFoundException cnfex) {
      SetMessages(cnfex.toString());
      cnfex.printStackTrace();
    } catch (IOException ioex) {
      SetMessages(ioex.toString());
      ioex.printStackTrace();
    } catch (Exception ex) {
      SetMessages(ex.toString());
      ex.printStackTrace();
    } finally {
      if (is != null) {
        is.close();
      }

      if (fc != null) {
        fc.close();
      }
    }

    return sb.toString();
  }
Ejemplo n.º 4
0
  public int postViaHttpConnection(String url) { // throws IOException,
    // RecordStoreException {

    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;
    int rc = -1;
    int recID = 0;
    RecordEnumeration recIter = null;
    SigSeg sigSeg = null;
    this.log("Test1");
    try {
      try {
        c = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
      } catch (IllegalArgumentException e) {
        this.alertError("post:open IllegalArgument: parameter is invalid. " + e.getMessage());
        throw e;
      } catch (ConnectionNotFoundException e) {
        this.alertError(
            "post:open ConnectionNotFound: target not found, or protocol not supported. "
                + e.getMessage());
        throw e;
      } catch (IOException e) {
        this.alertError("post:open IOException: " + e.getMessage());
        throw e;
      } catch (SecurityException e) {
        this.alertError("post:open SecurityException: " + e.getMessage());
        throw e;
      }
      this.log("Test2");

      try {
        c.setRequestMethod(HttpConnection.POST);
      } catch (IOException e) {
        this.alertError(
            "post:setReqMethod IOException: the method cannot be reset or if the requested method isn't valid for HTTP:"
                + e.getMessage());
        throw e;
      }
      this.log("Test3");

      try {
        c.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
        c.setRequestProperty("Accept", "text/plain");
        c.setRequestProperty("Accept-Encoding", "identity");
      } catch (IOException e) {
        this.alertError(
            "post:setReqProperty IOException: connection is in connected state." + e.getMessage());
        throw e;
      }
      this.log("Test4");

      try {
        os = c.openOutputStream();
      } catch (IOException e) {
        this.alertError(
            "post:openOutputStream IOException: maybe output stream has been closed?"
                + e.getMessage());
        throw e;
      }
      this.log("Test5");

      StringBuffer postBuf = new StringBuffer();
      postBuf.append("email=adparker%40gmail.com");
      postBuf.append("&pw=ecopda");
      postBuf.append("&type=xml");
      postBuf.append("&project_id=43");
      postBuf.append("&tableName=test2");
      postBuf.append("&data_string=");

      try {
        recIter = this.recordStore.enumerateRecords(null, null, true);
      } catch (RecordStoreNotOpenException e) {
        this.alertError("post:enumrateRecords RecordStoreNotOpenException" + e.getMessage());
        throw e;
      }
      this.log("Test6");

      try {
        recID = recIter.nextRecordId();
      } catch (InvalidRecordIDException e) {
        this.alertError("post:nextRecordId: no more records." + e.getMessage());
        throw e;
      }
      this.log("Test7");

      try {
        sigSeg = new SigSeg(this.recordStore, recID);
      } catch (RecordStoreNotOpenException e) {
        alertError("post:SigSeg RecordStoreNotOpen "); // +
        // e.getMessage());
        throw e;
      } catch (InvalidRecordIDException e) {
        alertError("post:SigSeg InvalidIDException "); // +
        // e.getMessage());
        throw e;
      } catch (RecordStoreException e) {
        alertError("post:SigSeg RecordStoreException"); // ";//+
        // e.getMessage());;
        throw e;
      } catch (IOException e) {
        alertError("post:SigSeg IOException " + e.getMessage());
        throw e;
      }
      this.log("Test8");

      postBuf.append(URLEncode.encode("<table>"));
      postBuf.append(URLEncode.encode("<row>"));

      // <field name="user">ASDFASDF</field>
      postBuf.append(URLEncode.encode("<field name=\"user\">"));
      String _userName = this.midlet.strItem_userName.getString();
      postBuf.append(URLEncode.encode(_userName));
      postBuf.append(URLEncode.encode("</field>"));

      postBuf.append(URLEncode.encode(sigSeg.toXML()));
      // postBuf.append(sigSeg.toXML());
      // sigSeg.toXML();

      postBuf.append(URLEncode.encode("</row>"));
      postBuf.append(URLEncode.encode("</table>"));

      // URL encode!

      try {
        // String urlenc = URLEncode.encode(postBuf.toString());
        // String urlenc = postBuf.toString();
        os.write(postBuf.toString().getBytes());
      } catch (IOException e) {
        alertError("post:os.write IOException " + e.getMessage());
        throw e;
      }
      this.log("Test9");

      try {
        os.flush();
      } catch (IOException e) {
        alertError("post:os.flush IOException " + e.getMessage());
        throw e;
      }
      this.log("Test10");

      try {
        rc = c.getResponseCode();
      } catch (IOException e) {
        alertError("post:c.getResponseCode IOException" + e.getMessage());
        throw e;
      }
      this.log("Test11");
      if (rc != HttpConnection.HTTP_OK) {
        this.alertError("HTTP response code: " + String.valueOf(rc));
      } else {
        ++this.int_recordsSent;
      }
      this.popRecord(recID);
      this.updateView();

    } catch (Exception e) {
    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      if (os != null)
        try {
          os.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      if (c != null)
        try {
          c.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
    return rc;
  }
  public boolean WriteLocalFile(String sWriteData, String sFileURL) throws IOException {
    FileConnection fc = null;
    DataOutputStream dos = null;
    // OutputStream os = null;
    // PrintStream ps = null;

    try {
      // If exists already, first delete file, a little clumsy.
      // fc = (FileConnection) Connector.open(fileURL.toString(), Connector.READ_WRITE);
      // String url = sFileSchema + sFSRoot + "expeout_201101" + sFileExt;
      String url = sFileSchema + sFSRoot + sLocalOutputPrefix + "201102" + sOutputExt;
      // fc = (FileConnection) Connector.open(url, Connector.READ_WRITE);
      fc = (FileConnection) Connector.open(sFileURL, Connector.READ_WRITE);
      // fc = (FileConnection) Connector.open(url, Connector.WRITE);
      if (!fc.exists()) {
        fc.create();
      }

      fc.truncate(0);

      dos = new DataOutputStream(fc.openOutputStream());
      // dos.writeUTF(sWriteData);
      // dos.write("ABCDE".getBytes());
      dos.write(sWriteData.getBytes());
      // os = fc.openOutputStream(Long.MAX_VALUE);
      // os = fc.openOutputStream();
      // ps = new PrintStream(os);
      // ps.println(sWriteData);

      SetSuccess(true);
    } catch (ConnectionNotFoundException cnfex) {
      SetSuccess(false);
      SetMessages(cnfex.toString());
      cnfex.printStackTrace();
    } catch (IOException ioex) {
      SetSuccess(false);
      SetMessages(ioex.toString());
      ioex.printStackTrace();
    } catch (Exception ex) {
      SetSuccess(false);
      SetMessages(ex.toString());
      ex.printStackTrace();
    } finally {
      /*if (ps != null) {
          ps.flush();
          ps.close();
      }
      if (os != null) {
          os.flush();
          os.close();
      }*/
      if (dos != null) {
        dos.flush();
        dos.close();
      }
      if (fc != null) {
        fc.close();
      }
    }

    return GetSuccess();
  }
  public String ReadRemoteFile() throws IOException {
    HttpConnection hc = null;
    // DataInputStream dis = null;
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    byte[] data = null;
    String sResult = "";
    StringBuffer sb = new StringBuffer();
    int length = 0;

    try {
      // String local_code = ReadLocalCode("expe");
      String local_code = "201101";
      String url = sHttpSchema + sRemoteRoot + sLocalExpePrefix + local_code + sLocalDataExt;
      hc = (HttpConnection) Connector.open(url);
      hc.setRequestMethod(HttpConnection.GET);

      int iResponseCode = hc.getResponseCode();
      if (iResponseCode == HttpConnection.HTTP_OK) {
        is = hc.openInputStream();
        length = (int) hc.getLength();

        if (length != -1) {
          data = new byte[length];
          is.read(data);
          sResult = new String(data);
        } else {
          baos = new ByteArrayOutputStream();
          int ch;
          while ((ch = is.read()) != -1) {
            baos.write(ch);
          }
          sResult = new String(baos.toByteArray());
        }
        System.out.println(sResult);
      } else {
        sResult = "ERROR: NO iResponseCode == HttpConnection.HTTP_OK";
        System.out.println(sResult);
      }

      /*if (length != -1) {
      data = new byte[length];
      dis = new DataInputStream(hc.openInputStream());
      //dis.readFully(data);
      } else {
      SetMessages("ERROR: Content length not given.");
      }*/
      /*int ch;
      while ((ch = is.read()) != -1) {
      sb.append((char) ch);
      }*/

      // sResult = sb.toString();
      // System.out.println(sResult);
    } catch (ConnectionNotFoundException cnfex) {
      SetMessages(cnfex.toString());
      sResult = GetMessages();
      cnfex.printStackTrace();
    } catch (IOException ioex) {
      SetMessages(ioex.toString());
      sResult = GetMessages();
      ioex.printStackTrace();
    } catch (Exception ex) {
      SetMessages(ex.toString());
      sResult = GetMessages();
      ex.printStackTrace();
    } finally {
      if (baos != null) {
        baos.close();
      }

      if (is != null) {
        is.close();
      }

      if (hc != null) {
        hc.close();
      }
    }

    return sResult;
  }