【问题标题】:.getResponseCode() hangs when trying to connect java(blackberry project) to webservice.getResponseCode() 尝试将 java(黑莓项目)连接到 web 服务时挂起
【发布时间】:2011-09-12 11:45:15
【问题描述】:

我正在尝试从 Eclipse 中的黑莓项目连接到 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.getReponse() 调用上挂起。它在黑莓 9800 模拟器上运行。任何帮助将不胜感激,因为我非常卡住

【问题讨论】:

    标签: java blackberry blackberry-eclipse-plugin httpconnection


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。可能会帮助其他人,但实际上我的解决方案在其他地方。在 Eclipse 中,我不得不去运行>调试配置>模拟器>并检查“启动移动数据系统模拟器。我觉得这很奇怪,默认情况下没有选中,但是哦,希望这比我花的时间节省更多的时间。跨度>
    【解决方案2】:

    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 减号,你可以看看:http://www.versatilemonkey.com/HttpConnectionFactory.java。它并不完美,但它是开始为 5.0 之前的平台创建连接的好地方。

    【讨论】:

    • 谢谢,我忘了说那部分
    【解决方案3】:

    也许尝试在 URL 的最后添加 ;deviceside=true ?对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多