开发者

.getResponseCode() hangs when trying to connect java(blackberry project) to webservice

I am trying to connect to a webservice from my blackberry project in eclipse. My code for URLConnector is the following

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.ui.component.Dialog;


public class URLConnector 
{
HttpConnection con = null; 
InputStream is = null; 

public URLConnector()   {
    try 
    {
    Dialog.inform("1"); 
    String url = new String("https://webserviceBlahBlah");

    Dialog.inform(con.toString()); 
        int responseCode = con.getResponseCode(); 
        Dialog.inform("3"); 
        if (responseCode != HttpConnection.HTTP_OK) { 
            Dialog.inform("3.5"); 
            System.out.println(responseCode); 
        } 
        is = con.openInputStream(); 
        byte[] responseData = new byte[10000]; 
        int length = 0; 
        Dialog.inform("4"); 
        StringBuffer rawResponse = new StringBuffer(); 
        while (-1 != (length = is.read(responseData))) { 
   开发者_StackOverflow         Dialog.inform("5"); 
            rawResponse.append(new String(responseData, 0, length)); 
        } 
        final String result = rawResponse.toString(); 
        System.out.println(result); 
        Dialog.inform("6"); 
    } 
    catch (Exception ex) 
    { 
        Dialog.inform("ex.getMessage()"); 
        System.out.println(ex.getMessage()); 
    } 
    finally 
    { 
        try { 
            is.close(); 
            is = null; 
            con.close(); 
            con = null; 
            Dialog.inform("8"); 
        } 
        catch(Exception e){
            Dialog.inform("e"); 
        } 
    } 
}

The application hangs on the con.getReponse() call. It is running on the blackberry 9800 simulator. Any help would be super appreciated because I am very stuck


getResponseCode() shouldn't be causing it to get stuck, as it simply reads the response that it got after making the connection. My guess is its actually the connection attempt that is hanging. If you're developing for 5.0 or higher, take a look at the ConnectionFactory class to make sure your connection is being created properly.


James, I'm assuming after you turned the MDS Simulator on, you discovered that you also had a problem because you never opened the connection?

Anyway just to give a final working solution:

    HttpConnection con = null;
    InputStream is = null;
    try {
        System.out.println("1");
        String url = new String("http://myserver.com/index.html");

        //I added these two lines to your code and tested it with my own server and got a 200 RC.
        net.rim.device.api.io.transport.ConnectionFactory cf = new net.rim.device.api.io.transport.ConnectionFactory();
        con = (HttpConnection)cf.getConnection(url).getConnection();

        System.out.println(con.toString());
        int responseCode = con.getResponseCode();
        System.out.println("3 responseCode = " + responseCode);
        if (responseCode != HttpConnection.HTTP_OK) {
            System.out.println("3.5");
            System.out.println(responseCode);
        }

Also if you have to support BBJava 5.0 minus, you can have a look at : http://www.versatilemonkey.com/HttpConnectionFactory.java . It's not prefect but its a good place to start with creating connections for pre 5.0 platforms.


Maybe try adding ;deviceside=true at the very end of the URL? Helped in my case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜