2011-05-18 66 views
-1

HTTP连接数据我写了这样的事情:无法访问得到的blackbery

package mypackage; 

import java.io.InputStream; 

import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.io.StreamConnection; 

import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.RichTextField; 
import net.rim.device.api.ui.container.MainScreen; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class MyScreen extends MainScreen implements FieldChangeListener 
{ 
    private static String HEADER_CONTENTTYPE = "content-type"; 
    private static String CONTENTTYPE_TEXTHTML = "text/html"; 
    String content = ""; 
    private static final int STATE_0 = 0; 
    private static final int STATE_1 = 1; 
    private static final int STATE_2 = 2; 
    private static final int STATE_3 = 3; 
    private static final int STATE_4 = 4; 
    private static final int STATE_5 = 5; 
    private static final char HTML_TAG_OPEN = '<'; 
    private static final char HTML_TAG_CLOSE = '>'; 
    private static final char CR = 0x000D; 
    private static final char LF = 0x000A; 
    private static final char TAB = 0x0009; 
    /** 
    * Creates a new MyScreen object 
    */ 
    public MyScreen() 
    { 
     // Set the displayed title of the screen 
     setTitle("MyTitle"); 

     final ButtonField bf = new ButtonField("New"); 
     FieldChangeListener listener = new FieldChangeListener() 
     { 
      public void fieldChanged(Field field, int context) //respond 
to button events 
      { 
       if(context != PROGRAMMATIC) 
       { 
        if(field==bf) 
        { 
        //System.out.println("=============================="); 
         fun1(); 
        } 
       } 
      } 
     }; 
     RichTextField rtf = new RichTextField("hello"); 
     bf.setChangeListener(listener); 
     add(bf); 
    } 

    void fun1() 
    { 
     try 
     { 
      System.out.println("asd"); 
      //Dialog.alert("asd"); 
      String str = 
fun_dwn_txt("www.google.co.in"); 
      Dialog.alert(str); 
     } 
     catch(Exception e) 
     {   Dialog.alert("Sorry"); 
      System.out.println("asd"); 

     } 
    } 

    String fun_dwn_txt(String str_url) throws Exception 
    { 
     StreamConnection s = null; 
     s = (StreamConnection)Connector.open(str_url); 
     HttpConnection httpConn = (HttpConnection)s; 

     int status = httpConn.getResponseCode(); 

     if (status == HttpConnection.HTTP_OK) 
     { 
      // Is this html? 
      String contentType = httpConn.getHeaderField(HEADER_CONTENTTYPE); 
      boolean htmlContent = (contentType != null && 
contentType.startsWith(CONTENTTYPE_TEXTHTML)); 

      InputStream input = s.openInputStream(); 

      byte[] data = new byte[256]; 
      int len = 0; 
      int size = 0; 
      StringBuffer raw = new StringBuffer(); 

      while (-1 != (len = input.read(data))) 
      { 
       // Exit condition for the thread. An IOException is 
       // thrown because of the call to httpConn.close(), 
       // causing the thread to terminate. 
//    if (_stop) 
//    { 
//     httpConn.close(); 
//     s.close(); 
//     input.close(); 
//    } 
       raw.append(new String(data, 0, len)); 
       size += len; 
      } 

      raw.insert(0, "bytes received]\n"); 
      raw.insert(0, size); 
      raw.insert(0, '['); 
      content = raw.toString(); 

      if (htmlContent) 
      { 
       content = prepareData(raw.toString()); 
      } 
      input.close(); 
     } 
     else 
     { 
      content = "No"; 
     } 
     s.close(); 
     return content; 
    } 

    private String prepareData(String text) 
    { 
     final int text_length = text.length(); 
     StringBuffer data = new StringBuffer(text_length); 
     int state = STATE_0; 
     int count = 0; 
     int writeIndex = -1; 
     char c = (char)0; 

     for (int i = 0; i < text_length; ++i) 
     { 
      c = text.charAt(i); 
      switch (state) 
      { 
       case STATE_0: 
        if (c == HTML_TAG_OPEN) 
        { 
         ++count; 
         state = STATE_1; 
        } 
        else if (c == ' ') 
        { 
         data.insert(++writeIndex, c); 
         state = STATE_5; 
        } 
        else if (!specialChar(c)) 
        { 
         data.insert(++writeIndex, c); 
        } 
        break; 

       case STATE_1: 
        if (c == '!' && text.charAt(i + 1) == '-' && 
text.charAt(i + 2) == '-') 
        { 
         System.out.println("Entering Comment state"); 
         i += 2; 
         state = STATE_3; 
        } 
        else if (Character.toLowerCase(c) == 'p') 
        { 
         state = STATE_4; 
        } 
        else if (c == HTML_TAG_CLOSE) 
        { 
         --count; 
         state = STATE_0; 
        } 
        else 
        { 
         state = STATE_2; 
        } 
        break; 

       case STATE_2: 
        if (c == HTML_TAG_OPEN) 
        { 
         ++count; 
        } 
        else if (c == HTML_TAG_CLOSE) 
        { 
         if(--count == 0) 
         { 
          state = STATE_0; 
         } 
        } 
        break; 

       case STATE_3: 
        if (c == '-' && text.charAt(i+1) == '-' && 
text.charAt(i + 2) == HTML_TAG_CLOSE) 
        { 
         --count; 
         i += 2; 
         state = STATE_0; 
         System.out.println("Exiting comment state"); 
        } 
        break; 

       case STATE_4: 
        if (c == HTML_TAG_CLOSE) 
        { 
         --count; 
         data.insert(++writeIndex, '\n'); 
         state = STATE_0; 
        } 
        else 
        { 
         state = STATE_1; 
        } 
        break; 

       case STATE_5: 
        if (c == HTML_TAG_OPEN) 
        { 
         ++count; 
         state = STATE_1; 
        } 
        else if (c != ' ') 
        { 
         state = STATE_0; 
         if (!specialChar(c)) 
         { 
          data.insert(++writeIndex, c); 
         } 
        } 
        break; 
      } 
     } 

     return data.toString().substring(0, writeIndex + 1); 
    } 

    private boolean specialChar(char c) 
    { 
     return c == LF || c == CR || c == TAB; 
    } 

    public void fieldChanged(Field field, int context) { 
     // TODO Auto-generated method stub 

    } 
} 

但按钮点击什么也没有发生。即使HttpConnection.HTTP_OK没有响应..

上午我丢失了一些细节,我在XML中具有互联网连接提...

请建议什么......

+0

yes可能更容易,“status”可能不是“HTTP_OK”。尝试打印出来 – asgs 2011-05-18 13:55:11

+0

除非我误解了你的程序逻辑,否则你正在调用在事件线程上读取HTTP输入流。这不是在BlackBerry平台上执行此类操作的方式。您不应该在事件线程上调用阻塞方法。 – Richard 2011-05-18 13:59:42

+0

实际的谷歌是不是实际的链接,其192.168.1.1/mobileapp/default.aspx虚拟目录我在我的电脑中创建... – Sourabh 2011-05-18 16:11:38

回答

2
  • www.google.co.in不是网址。您需要使用http://www.google.co.inhttps://www.google.co.in

  • 您没有指定连接方法(BES,BIS-B,WiFi等)。虽然这可能起作用,但它并不总是有效。你使用WLAN吗?然后在连接字符串中使用;interface=wifi后缀。 BES?使用;deviceside=false等。

有关详细信息,请参阅this article。在BlackBerry中使用连接器API并不容易。如果您有更新的BlackBerry(设备软件版本5.0+),那么使用Network API

+0

代码中还存在其他问题,但这两个与连接直接相关。 – hrnt 2011-05-18 14:02:16