无法访问从黑莓中的http连接获取数据

时间:2011-05-18 13:52:26

标签: java http blackberry connection

我写过这样的话:

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中提及互联网连接...

请提出建议......

1 个答案:

答案 0 :(得分: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中使用Connector API并不容易。如果您有更新的BlackBerry(设备软件版本5.0+),那么使用Network API

可能会更容易