None of code can establish http connection over BIS

吃可爱长大的小学妹 提交于 2019-12-11 16:22:45

问题


I am new in developing Blackberry Application.

In these three days, I already searched and learned in both forum and tutorial from the RIM itself. But none of them can solve my problem. >.<

So. I already tried some different methods to establish http connection over BIS in 4.6.

These are the following codes: 1. HttpConnection httpConnection;

    String url = "myURL;deviceside=true";

    try{
    httpConnection = (HttpConnection) Connector.open(url);
    Dialog.inform(">.<");
    }

    catch(Exception e)
    {
        Dialog.inform(e.getMessage());
    }

From the code #1 above, none of the dialogs are displayed.

  1. String url = "myURL";
    
    
    try {
    
        StreamConnection s = (StreamConnection)Connector.open(url);
    
        InputStream input = s.openInputStream();
    
        Dialog.inform("sblm byte");
    
        byte[] data = new byte[256];
        int len = 0;
        StringBuffer raw = new StringBuffer();
    
        Dialog.inform("stlh buat byte");
    
        while( -1 != (len = input.read(data))) {
            raw.append(new String(data, 0, len));
        }
    
        Dialog.inform("stlh while");
        response = raw.toString();
        Dialog.inform(response);
    
        input.close();
        s.close();
    
    } 
    
            catch(Exception e) { }
    

As well as code #1, this code above also doesnt pop up any dialog.

I am desperately need the right guide for establishing simple http connection. Is there any technique that I missed? Do I need any signature for this? Do I need extra setting in both my Blackberry device (BB 8900 with OS 5.00) or in my compiler, Eclipse?

Thank you.


回答1:


Try this code.

 try {

    HttpConnection httpConnection=(HttpConnection)Connector.open(url);
    httpConnection.setRequestMethod(HttpConnection.GET);
   if(httpConnection.getResponseCode()==HttpConnection.HTTP_OK)
   {
     InputStream is=httpConnection.openInputStream();
     int ch;
     StringBuffer buffer=new StringBuffer();
       while((ch=is.read())!=-1)
       {
        buffer.append((char)ch);
      }
     }
    } catch (IOException e) {
        System.out.println("Exception From Thread"+e);
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/7709669/none-of-code-can-establish-http-connection-over-bis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!