Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.getResponseCode()在尝试将java(blackberry项目)连接到webservice时挂起_Java_Blackberry_Blackberry Eclipse Plugin_Httpconnection - Fatal编程技术网

.getResponseCode()在尝试将java(blackberry项目)连接到webservice时挂起

.getResponseCode()在尝试将java(blackberry项目)连接到webservice时挂起,java,blackberry,blackberry-eclipse-plugin,httpconnection,Java,Blackberry,Blackberry Eclipse Plugin,Httpconnection,我正在尝试从eclipse中的blackberry项目连接到Web服务。我的URLConnector代码如下 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 { Http

我正在尝试从eclipse中的blackberry项目连接到Web服务。我的URLConnector代码如下

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))) { 
            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"); 
        } 
    } 
}

应用程序挂起con.getResponse()调用。它在黑莓9800模拟器上运行。任何帮助都将不胜感激,因为我被卡住了

getResponseCode()不应该导致它卡住,因为它只是读取连接后得到的响应。我猜这实际上是挂起的连接尝试。如果您正在开发5.0或更高版本,请查看该类以确保正确创建连接。

James,我假设在您打开MDS模拟器后,您发现您也有问题,因为您从未打开连接

无论如何,只是给出一个最终的工作解决方案:

    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);
        }

另外,如果您必须支持BBJava 5.0减号,您可以查看:。它不是完美的,但它是一个开始为5.0之前的平台创建连接的好地方

可以尝试添加;deviceside=在URL的最末端为true?对我的情况有帮助。

谢谢您的回复。可能会帮助其他人,但实际上我的解决方案在别处。在eclipse中,我必须运行>调试配置>模拟器>并选中“启动移动数据系统模拟器”。我发现这在默认情况下没有被选中很奇怪,但哦,希望这比我花的时间节省了更多的时间。谢谢,我忘了提那部分