Package javax.microedition.io

Examples of javax.microedition.io.HttpConnection


    // Get all user comments
    Comments userComments(long userId, String sort) {
   
    String urlToRequest       = "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/comments?sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomCommentsParser commentParser = new DomCommentsParser();
        commentParser.m_Xml = xml;       
        Comments comments = commentParser.parse();
        content.close();
View Full Code Here


    // Get all photo comments
    Comments photoComments(long photoId, String sort) {
   
    String urlToRequest       = "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/comments?sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomCommentsParser commentParser = new DomCommentsParser();
        commentParser.m_Xml = xml;       
        Comments comments = commentParser.parse();
        content.close();
View Full Code Here

    // Get photo comments
    Comments photoComments(long photoId, int ps, int ind, String sort) {
   
    String urlToRequest       = "http://tweetphotoapi.com/api/tpapi.svc/photos/" + photoId + "/comments?ps=" + ps + "&ind=" + ind + "&sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomCommentsParser commentParser = new DomCommentsParser();
        commentParser.m_Xml = xml;       
        Comments comments = commentParser.parse();
        content.close();
View Full Code Here

    // Get favorites
    Favorites favorites(long userId, int ps, int ind, String sort) {
   
    String urlToRequest       = "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites?ind=" + ind + "&ps=" + ps + "&sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomFavoritesParser favoritesParser = new DomFavoritesParser();
        favoritesParser.m_Xml = xml;       
        Favorites favorites = favoritesParser.parse();
        content.close();
View Full Code Here

    // Get all favorites
    Favorites favorites(long userId, String sort) {
   
    String urlToRequest = "http://tweetphotoapi.com/api/tpapi.svc/users/" + userId + "/favorites?sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomFavoritesParser favoritesParser = new DomFavoritesParser();
        favoritesParser.m_Xml = xml;       
        Favorites favorites = favoritesParser.parse();
        content.close();
View Full Code Here

    if (m_ServiceName.equalsIgnoreCase("Facebook") ) {
      urlToRequest = "http://tweetphotoapi.com/api/tpapi.svc/facebooksignin";
      }
   
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);
     
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
            System.out.println("Response: " + httpConn.getResponseMessage());
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomProfileParser profileParser = new DomProfileParser();
        profileParser.m_Xml = xml;
        Profile profile = (Profile)profileParser.parse();
       
View Full Code Here

  // Get the public feed (all)
  SocialFeed publicFeed(String sort) {
   
    String urlToRequest       = "http://tweetphotoapi.com/api/tpapi.svc/socialfeed?sort=" + sort + ";deviceside=true";
    try {
      HttpConnection httpConn = HttpUtils.makeHttpConnection(urlToRequest, new HttpHeaders(), null, getCoverageBasedConnectionType(), "GET", this, false);             
      m_httpStatus = httpConn.getResponseCode();
    
      if (m_httpStatus != 200) {
               System.out.println("Error: " + m_httpStatus);
      } else {
        InputStream content = httpConn.openInputStream();
        String xml = convertStreamToString(content);
        DomSocialFeedParser profileParser = new DomSocialFeedParser();
        profileParser.m_Xml = xml;
        SocialFeed feed = profileParser.parse();
        content.close();
View Full Code Here

  public void run() {
    readHttp();
  }

  private void readHttp() {
    HttpConnection c = null;
    DataInputStream dis = null;

    try {
      c = (HttpConnection) Connector.open(this.url);

      c.setRequestMethod(this.httpMethod);

      Enumeration keys = this.headers.keys();
      while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        c.setRequestProperty((String) key, (String) headers.get(key));
      }

      addVar("readyState", READY_STATE_LOADING);
      addVar("status", new Double(c.getResponseCode()));

      int len = (int) c.getLength();
      dis = c.openDataInputStream();
      if (len > 0) {
        this.responseBody = new byte[len];
        dis.readFully(this.responseBody);
        addVar("resBytes", this.responseBody);
        for (int i = 0; i < this.responseBody.length; i++) {
          // TODO: check MIME-type and handle charset correctly
          this.responseText.append((char) this.responseBody[i]);
        }
      } else {
        int ch;
        while ((ch = dis.read()) != -1) {
          // TODO: check MIME-type and handle charset correctly
          this.responseText.append(ch);
        }
      }
    } catch (IOException e) {
      // swallow exception as XHR open() method cannot throw error
      e.printStackTrace();
    } finally {
      if (dis != null)
        try {
          dis.close();
          if (c != null) {
            c.close();
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
    }
View Full Code Here

          "Requesting");

      int contentLength = -1;

      if (con instanceof HttpConnection) {
        HttpConnection httpCon = (HttpConnection) con;
        if (post) {
          httpCon.setRequestMethod("POST");
          httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        }
        httpCon.setRequestProperty("User-Agent", browser.userAgent);
        httpCon.setRequestProperty("X-Screen-Width", ""+screen.getWidth());
        httpCon.setRequestProperty("Connection", "Keep-Alive");
        httpCon.setRequestProperty("Accept-Charset", "utf-8");

        String cookie = browser.getCookies(url);
        if (cookie != null) {
//          System.out.println("Setting Cookie: " + cookie);
          httpCon.setRequestProperty("Cookie", cookie);
        }

        if (post && requestData != null) {
          OutputStream os = httpCon.openOutputStream();
          os.write(requestData);
          os.close();
//          System.out.println("Postdata\n" + new String(requestData));
        }

//        System.out.println("receiving:");
        contentLength = (int) httpCon.getLength();
        int headerIndex = 0;
        while (true){
          String name = httpCon.getHeaderFieldKey(headerIndex);
          if (name == null) {
            break;
          }
          name = name.toLowerCase();
//          System.out.println(name + ": " + httpCon.getHeaderField(headerIndex));
         
          if ("content-type".equals(name)) {
            String value = httpCon.getHeaderField(headerIndex);
            if (value.indexOf("vnd.sun.j2me.app-descriptor") != -1) {
              browser.platformRequest(url);
              return;
            }
            int eq = value.indexOf ("harset=");
            if (eq != -1) {
              encoding = value.substring(eq + 7).trim();
            }
          } else if ("set-cookie".equals(name)) {
            String cName = "";
            String cValue = "";
            String cHost = httpCon.getHost();
            String cPath = "";
            String cExpires = null;
            String cSecure = null;
            String[] parts = Util.split(
                httpCon.getHeaderField(headerIndex), ';');
            for (int i = 0; i < parts.length; i++){
              String part = parts[i];
              cut = part.indexOf('=');
              if (cut == -1) {
                continue;
              }
              String key = part.substring(0, cut).trim();
              String value = part.substring(cut + 1).trim();
              if (i == 0) {
                cName = key;
                cValue = value;
              } else {
                key = key.toLowerCase();
                if ("host".equals(key)) {
                  cHost = value;
                } else if ("path".equals(key)) {
                  cPath = value;
                } else if ("expires".equals(key)) {
                  cExpires = value;
                } else if ("secure".equals(key)) {
                  cSecure = value;
                }
              }          
            }
            browser.setCookie(cHost, cPath, cName, cValue, cExpires, cSecure);
          }
          headerIndex++;
        }

        int responseCode = httpCon.getResponseCode();
//        System.out.println("Response Code: " + httpCon.getResponseCode());
        if (responseCode >= 300 && responseCode <= 310) {
          String location = httpCon.getHeaderField("Location");
          if (location != null) {
//            System.out.println("Redirecting to: " + location);
          screen.htmlWidget.setUrl(location);
            browser.requestResource(screen.htmlWidget, method, location,
                type, requestData);
View Full Code Here

     * @return The HttpConnection object representing the connection to the
     *         resource, null if no connection could be made
     */
    public static HttpConnection makeConnection(final String url,
            final HttpHeaders requestHeaders, final byte[] postData) {
        HttpConnection conn = null;
        OutputStream out = null;

        try {
            conn = (HttpConnection) Connector.open(url);

            if (requestHeaders != null) {
                // From
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec15.html#sec15.1.3
                //
                // Clients SHOULD NOT include a Referer header field in a
                // (non-secure) HTTP
                // request if the referring page was transferred with a secure
                // protocol.
                final String referer =
                        requestHeaders.getPropertyValue("referer");
                boolean sendReferrer = true;

                if (referer != null
                        && StringUtilities.startsWithIgnoreCase(referer,
                                "https:")
                        && !StringUtilities.startsWithIgnoreCase(url, "https:")) {
                    sendReferrer = false;
                }

                int size = requestHeaders.size();
                for (int i = 0; i < size;) {
                    final String header = requestHeaders.getPropertyKey(i);

                    // Remove referer header if needed.
                    if (!sendReferrer && header.equals("referer")) {
                        requestHeaders.removeProperty(i);
                        --size;
                        continue;
                    }

                    final String value = requestHeaders.getPropertyValue(i++);
                    if (value != null) {
                        conn.setRequestProperty(header, value);
                    }
                }
            }

            if (postData == null) {
                conn.setRequestMethod(HttpConnection.GET);
            } else {
                conn.setRequestMethod(HttpConnection.POST);

                conn.setRequestProperty(
                        HttpProtocolConstants.HEADER_CONTENT_LENGTH, String
                                .valueOf(postData.length));

                out = conn.openOutputStream();
                out.write(postData);

            }
        } catch (final IOException e1) {
            errorDialog(e1.toString());
View Full Code Here

TOP

Related Classes of javax.microedition.io.HttpConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.