Java Code Examples for javax.microedition.io.HttpConnection#HTTP_OK

The following examples show how to use javax.microedition.io.HttpConnection#HTTP_OK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContentReader.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds the type of the content in this Invocation.
 * <p>
 * The calling thread blocks while the type is being determined.
 * If a network access is needed there may be an associated delay.
 *
 * @return the content type.
 *          May be <code>null</code> if the type can not be determined.
 *
 * @exception IOException if access to the content fails
 * @exception IllegalArgumentException if the content is accessed via
 *  the URL and the URL is invalid
 * @exception SecurityException is thrown if access to the content
 *  is required and is not permitted
 */
String findType() throws IOException, SecurityException {
    String type = null;
    Connection conn = openPrim(true);
    if (conn instanceof ContentConnection) {
    	if( conn instanceof HttpConnection ){
    		HttpConnection hc = (HttpConnection)conn;
         hc.setRequestMethod(HttpConnection.HEAD);
	
         // actual connection performed, some delay...
         if (hc.getResponseCode() != HttpConnection.HTTP_OK)
         	return null;
    	}
    	
        type = ((ContentConnection)conn).getType();
        conn.close();

        if (type != null) {
            // Check for and remove any parameters (rfc2616)
            int ndx = type.indexOf(';');
            if (ndx >= 0) {
                type = type.substring(0, ndx);
            }
            type = type.trim();
            if (type.length() == 0) {
                type = null;
            }
        }
    }

    return type;
}
 
Example 2
Source File: UpdateTask.java    From google-authenticator with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void run() {
  try {
    // Visit the original download URL and read the JAD;
    // if the MIDlet-Version has changed, invoke the callback.
    String url = Build.DOWNLOAD_URL;
    String applicationVersion = getApplicationVersion();
    String userAgent = getUserAgent();
    String language = getLanguage();
    for (int redirectCount = 0; redirectCount < 10; redirectCount++) {
      HttpConnection c = null;
      InputStream s = null;
      try {
        c = connect(url);
        c.setRequestMethod(HttpConnection.GET);
        c.setRequestProperty("User-Agent", userAgent);
        c.setRequestProperty("Accept-Language", language);

        int responseCode = c.getResponseCode();
        if (responseCode == HttpConnection.HTTP_MOVED_PERM
            || responseCode == HttpConnection.HTTP_MOVED_TEMP) {
          String location = c.getHeaderField("Location");
          if (location != null) {
            url = location;
            continue;
          } else {
            throw new IOException("Location header missing");
          }
        } else if (responseCode != HttpConnection.HTTP_OK) {
          throw new IOException("Unexpected response code: " + responseCode);
        }
        s = c.openInputStream();
        String enc = getEncoding(c);
        Reader reader = new InputStreamReader(s, enc);
        final String version = getMIDletVersion(reader);
        if (version == null) {
          throw new IOException("MIDlet-Version not found");
        } else if (!version.equals(applicationVersion)) {
          Application application = Application.getApplication();
          application.invokeLater(new Runnable() {
            public void run() {
              mCallback.onUpdate(version);
            }
          });
        } else {
          // Already running latest version
        }
      } finally {
        if (s != null) {
          s.close();
        }
        if (c != null) {
          c.close();
        }
      }
    }
  } catch (Exception e) {
    System.out.println(e);
  }
}