【问题标题】:BlackBerry simulator can connect to web service, but real device can't黑莓模拟器可以连接网络服务,但真机不能
【发布时间】:2012-01-13 10:40:38
【问题描述】:

我正在开发连接到网络服务的 BlackBerry 应用程序。

当我在模拟器上开发时,我使用 BlackBerry MDS 作为模拟器,一切都很顺利。我的应用程序(在模拟器上运行)可以完美地连接到我的 Web 服务。请注意,模拟器和 Web 服务位于不同的 PC 上。

现在,我的项目完成了。我尝试将我的应用程序部署到真实设备(BB 8520)。当我在设备上使用该应用程序时,我发现它无法连接到网络服务。我在互联网上进行了研究,我确信这一定是由于 MDS 问题。似乎我必须在我的 Web 服务所在的计算机上对 MDS 做一些事情,但我仍然找不到明显的答案。

请大家帮帮我...

PS。我的 Web 服务在 IIS 上发布,并在 Visual Studio 2010 中开发。BlackBerry 应用程序在 Eclipse 中开发,并通过 ksoap2 连接到 Web 服务。 Web 服务所在计算机上的防火墙已关闭。使用的连接是WIFI。

【问题讨论】:

  • 您是否检查了设备中的连接..?如果没问题。首先检查设备的默认浏览器以提供类似“google.com”的内容。如果浏览器不工作,那是你的设备问题。
  • 设备正在连接 WIFI,我可以在上面打开 google、Facebook 和任何网络。顺便说一句,我想我得到了一个线索。看来我需要在 Web 服务 URL 之后放置 ;deviceside = false 之类的参数。 (但我还没有尝试过。)

标签: web-services blackberry ksoap2 blackberry-eclipse-plugin


【解决方案1】:

如果您的设备使用 wifi 连接 ";interface=wifi" 发送到 URL。

【讨论】:

    【解决方案2】:

    您必须使用HTTPConnetion 将所有连接参数添加到您的网址中。创建一个自定义类来检查连接参数。使用下面的Custom Class

    public class HttpConnectionImpl 
    extends impl.javame.com.twitterapime.io.HttpConnectionImpl {
    
    private static String appendConnectionParameters;
    
    private static String connectionParameters;
    
    
    private static ServiceRecord getWAP2ServiceRecord() {
        String cid;
        String uid;
        ServiceBook sb = ServiceBook.getSB();
        ServiceRecord[] records = sb.getRecords();
        //
        for (int i = records.length -1; i >= 0; i--) {
            cid = records[i].getCid().toLowerCase();
            uid = records[i].getUid().toLowerCase();
            //
            if (cid.indexOf("wptcp") != -1 
                    && uid.indexOf("wifi") == -1 
                    && uid.indexOf("mms") == -1) {
                return records[i];
            }
        }
        //
        return null;
    }
    
    
    public static String getConnectionParams() {
        String connParams = "";
        //
        if (connectionParameters != null) {
            connParams = connectionParameters;
        } else {
            if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
                connParams = ";interface=wifi"; //Connected to a WiFi access point.
            } else {
                int coverageStatus = CoverageInfo.getCoverageStatus();
                //
                if ((coverageStatus & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B) {
                    connParams = ";deviceside=false;ConnectionType=mds-public";
                } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
                    // Have network coverage and a WAP 2.0 service book record
                    ServiceRecord record = getWAP2ServiceRecord();
                    //
                    if (record != null) {
                        connParams = ";deviceside=true;ConnectionUID=" + record.getUid();
                    } else {
                        connParams = ";deviceside=true";
                    }
                } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
                    // Have an MDS service book and network coverage
                    connParams = ";deviceside=false";
                }
            }
            //
            if (appendConnectionParameters != null) {
                connParams += appendConnectionParameters;
            }
        }
        //
        return connParams;
    }
    
    
    public static void setAppendConnectionParameters(String params) {
        if (params != null && !params.startsWith(";")) {
            params = ";" + params;
        }
        //
        appendConnectionParameters = params;
    }
    
    
    public static void setConnectionParameters(String params) {
        if (params != null && !params.startsWith(";")) {
            params = ";" + params;
        }
        //
        connectionParameters = params;
    }
    
    
    public void open(String url) throws IOException {
        super.open(url + getConnectionParams());
    }
    }
    

    集成以上类后,您必须制作一个名称为impl.javame.com.twitterapime.io 的包并添加以下类。

    `package impl.javame.com.twitterapime.io; 导入 java.io.IOException; 导入 java.io.InputStream; 导入 java.io.OutputStream; 导入 javax.microedition.io.Connector; 导入 com.twitterapime.io.HttpConnection;

    公共类 HttpConnectionImpl 实现 HttpConnection { 私有 javax.microedition.io.HttpConnection httpConn;

    public void open(String url) throws IOException {
        httpConn =
            (javax.microedition.io.HttpConnection)
                Connector.open(url, Connector.READ_WRITE, true);
    }
    
    public void close() throws IOException {
        httpConn.close();
    }
    
    public int getResponseCode() throws IOException {
        return httpConn.getResponseCode();
    }
    public InputStream openInputStream() throws IOException {
        return httpConn.openInputStream();
    }
    
    public OutputStream openOutputStream() throws IOException {
        return httpConn.openOutputStream();
    }
    public void setRequestMethod(String method) throws IOException {
        httpConn.setRequestMethod(method);
    }
    public void setRequestProperty(String key, String value) throws IOException{
        httpConn.setRequestProperty(key, value);
    }
    public String getRequestProperty(String key) throws IOException {
        return httpConn.getRequestProperty(key);
    }
    public String getHeaderField(String name) throws IOException {
        return httpConn.getHeaderField(name);       
    }
    

    }`

    现在你必须整合另外两个类

    1. HttpConnection
    2. HttpResponsein com.twitterapime.io 包。

    `包com.twitterapime.io; 导入 java.io.IOException; 导入 java.io.InputStream; 导入 java.io.OutputStream; 公共接口 HttpConnection { public static final String GET = "GET";

    public static final String POST = "POST";
    
    public static final String HEAD = "HEAD";
    
    public static final int HTTP_OK = 200;
    
    public static final int HTTP_FORBIDDEN = 403;
    
    public static final int HTTP_UNAVAILABLE = 503;
    
    public static final int HTTP_NOT_MODIFIED = 304;
    
    public static final int HTTP_BAD_REQUEST = 400;
    
    public static final int HTTP_UNAUTHORIZED = 401;
    
    public static final int HTTP_NOT_FOUND = 404;
    
    public static final int HTTP_NOT_ACCEPTABLE = 406;
    
    public static final int HTTP_INTERNAL_ERROR = 500;
    
    public static final int HTTP_BAD_GATEWAY  = 502;
    
    public void open(String url) throws IOException;
    
    public void close() throws IOException;
    
    public int getResponseCode() throws IOException;
    
    public InputStream openInputStream() throws IOException;
    
    public OutputStream openOutputStream() throws IOException;
    
    public void setRequestMethod(String method) throws IOException;
    
    public void setRequestProperty(String key, String value) throws IOException;
    
    public String getRequestProperty(String key) throws IOException;
    
    public String getHeaderField(String name) throws IOException;
    

    Bwlow 是 HttpResponse 类`

    `包com.twitterapime.io;

    导入 java.io.ByteArrayOutputStream; 导入 java.io.IOException; 导入 java.io.InputStream; 导入 java.io.UnsupportedEncodingException;

    公共最终类 HttpResponse { 私有 int 代码;

    private String body;
    
    private InputStream stream;
    
    private HttpConnection conn;
    
    HttpResponse(HttpConnection conn) throws IOException {
        this.conn = conn;
        code = conn.getResponseCode();
        stream = conn.openInputStream();
    }
    
    public boolean wasSuccessful() {
        return code >= 200 && code < 400;
    }
    
    public String getBodyContent() throws IOException {
        return body != null ? body : (body = parseBody(stream));
    }
    
    public InputStream getStream() {
        return stream;
    }
    
    public int getCode() {
        return code;
    }
    
    public String getResponseField(String key) throws IOException {
        return conn.getRequestProperty(key);
    }
    
    private String parseBody(InputStream in) throws IOException {
        if (in == null) {
            return null;
        }
        //
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[1024];
        //
        for (int n; (n = in.read(buffer)) > 0;) {
            out.write(buffer, 0, n);
        }
        //
        try {
            return new String(out.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IOException(e.getMessage());
        }
    }
    

    }`

    使用以下方法检查您的连接参数。此方法会自动检查您的设备连接可用性并根据您的连接添加连接参数。

    protected HttpConnection getConnection(String url) throws IOException {
        url += HttpConnectionImpl.getConnectionParams();
        //
        return (HttpConnection)Connector.open(url);
    }
    

    上面的HttpConnection 方法返回带有连接参数的 URL .. 比你可以在你的 InputStream 中使用这个连接并打开任何 url 以及 web 服务。

    希望对你有帮助……

    【讨论】:

      【解决方案3】:

      谢谢大家。我设法通过将 ;interface=wifi;deviceside=false 添加到 URL 来解决这个问题。 :D

      【讨论】:

        【解决方案4】:

        我有同样的问题,我通过使用 Networkutils 获取连接字符串并将此连接字符串附加到 BaseUrl 解决了这个问题,我的问题得到了解决。我认为这也可以解决你的问题。而NetworkUtils类如下...

        public  String formatURL(String httpBaseURL) {
                initializeTransportAvailability();
        
                /**
                 * Rest of this method simply tries to communicate over different transports
                 * that are available. For each transport it will retry 'retries' many times if
                 * an attempt fails.
                 */
                /** Direct TCP using HTTP */
                if (coverageTCP) {
                    System.out.println("Direct TCP (HTTP)");
                    return getTCPURL(httpBaseURL);
                }
        
                if (srMDS != null && coverageMDS) {
                    System.out.println("MDS (HTTP)");
                    return getMDSURL(httpBaseURL);
                } else {
                    if (srMDS == null) {
                        System.out.println("No MDS service records found.");
                    }
                    if (!coverageMDS) {
                        System.out.println("MDS coverage is not available");
                    }
                }
        
                if (srUnite != null && coverageUnite) {
                    System.out.println("Unite (HTTP)");
                    return getUniteURL(httpBaseURL);
                } else {
                    if (srUnite == null) {
                        System.out.println("No Unite service records found.");
                    }
                    if (!coverageUnite) {
                        System.out.println("Unite coverage is not available");
                    }
                }
        
                if (srBIS != null && coverageBIS) {
                    System.out.println("BIS-B (HTTP)");
                    return getBISURL(httpBaseURL);
                } else {
                    if (srBIS == null) {
                        System.out.println("No BIS-B service records found.");
                    }
                    if (!coverageBIS) {
                        System.out.println("BIS-B coverage is not available");
                    }
                }
        
                getWAPURL(httpBaseURL);
                if (srWAP != null && !wapParametersUnavailable && coverageWAP) {
                    System.out.println("WAP (HTTP)");
                    return getWAPURL(httpBaseURL);
                } else {
                    if (srWAP == null) {
                        System.out.println("No WAP service records found.");
                    }
                    if (!coverageWAP) {
                        System.out.println("WAP coverage is not available");
                    }
                    if (wapParametersUnavailable) {
                        System.out.println("Please provide WAP parameters");
                    }
                }
        
                if (srWAP2 != null && coverageWAP2) {
                    System.out.println("WAP2 (HTTP)");
                    return getWAP2URL(httpBaseURL);
                } else {
                    if (srWAP2 == null) {
                        System.out.println("No WAP2 service records found.");
                    }
                    if (!coverageWAP2) {
                        System.out.println("WAP2 coverage is not available");
                    }
                }
        
                if (srWiFi != null && coverageWiFi) {
                    System.out.println("WiFi (HTTP)");
                    return getWiFiURL(httpBaseURL);
                } else {
                    if (srWiFi == null) {
                        System.out.println("No WiFi service records found.");
                    }
                    if (!coverageWiFi) {
                        System.out.println("WiFi coverage is not available");
                    }
                }
        
                return httpBaseURL;
            }
        
            /**
             * Initializes the ServiceRecord instances for each transport (if available). Otherwise leaves it null.
             * Also determines if sufficient coverage is available for each transport and sets coverage* flags.
             */
            private void initializeTransportAvailability() {
                ServiceBook sb = ServiceBook.getSB();
                ServiceRecord[] records = sb.getRecords();
        
                for (int i = 0; i < records.length; i++)
                { 
                    ServiceRecord myRecord = records[i];
                    String cid, uid;
        
                    if (myRecord.isValid() && !myRecord.isDisabled()) {
                        cid = myRecord.getCid().toLowerCase();
                        uid = myRecord.getUid().toLowerCase();
                        // BIS
                        if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") != -1) {
                            srBIS = myRecord;
                        }
        
                        // BES
                        if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") == -1) {
                            srMDS = myRecord;
                        }
                        // WiFi
                        if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") != -1) {
                            srWiFi = myRecord;
                        }
                        // Wap1.0
                        if (getConfigType(myRecord) == CONFIG_TYPE_WAP && cid.equalsIgnoreCase("wap")) {
                            srWAP = myRecord;
                        }
                        // Wap2.0
                        if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 && uid.indexOf("mms") == -1) {
                            srWAP2 = myRecord;
                        }
                        // Unite
                        if (getConfigType(myRecord) == CONFIG_TYPE_BES && myRecord.getName().equals(UNITE_NAME)) {
                            srUnite = myRecord;
                        }
                    }
                }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) {
                    coverageBIS = true;
                    System.out.println("Coverage Status: BIS-B - Online");
                }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) {
                    coverageTCP = true;
                    System.out.println("Coverage Status: COVERAGE_DIRECT - Online");
                    coverageWAP = true;
                    System.out.println("Coverage Status: WAP 1.0, 1.1 - Online");
                    coverageWAP2 = true;
                    System.out.println("Coverage Status: WAP 2.0 - Online");
                }
                if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)) {
                    coverageMDS = true;
                    System.out.println("Coverage Status: MDS - Online");
                    coverageUnite = true;
                    System.out.println("Coverage Status: Unite - Online");
                }
        
                if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
                    coverageWiFi = true;
                    System.out.println("Coverage Status: Wifi - Online");
                }
            }
        
            /**
             * Gets the config type of a ServiceRecord using getDataInt below
             * @param record A ServiceRecord
             * @return configType of the ServiceRecord
             */
            private int getConfigType(ServiceRecord record) {
                return getDataInt(record, 12);
            }
        
            /**
             * Gets the config type of a ServiceRecord. Passing 12 as type returns the configType.
             * @param record A ServiceRecord
             * @param type dataType
             * @return configType
             */
            private int getDataInt(ServiceRecord record, int type) {
                DataBuffer buffer = null;
                buffer = getDataBuffer(record, type);
        
                if (buffer != null) {
                    try {
                        return ConverterUtilities.readInt(buffer);
                    } catch (EOFException e) {
                        return -1;
                    }
                }
                return -1;
            }
        
            /**
             * Utility Method for getDataInt()
             */
            private DataBuffer getDataBuffer(ServiceRecord record, int type) {
                byte[] data = record.getApplicationData();
                if (data != null) {
                    DataBuffer buffer = new DataBuffer(data, 0, data.length, true);
                    try {
                        buffer.readByte();
                    } catch (EOFException e1) {
                        return null;
                    }
                    if (ConverterUtilities.findType(buffer, type)) {
                        return buffer;
                    }
                }
                return null;
            }
        
            /**
             * Constructs a Direct TCP url from the baseURL provided by the user
             * @return A url with Direct TCP parameters
             */
            private String getTCPURL(String baseURL) {
        
                UiApplication.getUiApplication().invokeAndWait(new Runnable() {
        
                    public void run() {
                        // TODO Auto-generated method stub
        
                        if(GCAppSettings.APN.trim().length() < 2 && (!apnDialogueShown)){
                            String[] buttons = { "Ok", "Cancel" };
                            Dialog dialog = new Dialog("", buttons, null, 0, Bitmap.getPredefinedBitmap(Bitmap.INFORMATION));
        
                            EditField apnField = new EditField("APNName: ", "", 
                                    TextField.DEFAULT_MAXCHARS, EditField.NO_NEWLINE);
                            dialog.add(apnField);
        
                            BasicEditField usernameField = new BasicEditField("Username: ", "", 
                                    15,EditField.EDITABLE);
                            dialog.add(usernameField);
                            PasswordEditField passwordField = new PasswordEditField("Password: ", "", 
                                    15, EditField.EDITABLE);
                            dialog.add(passwordField);
        
                            if (dialog.doModal() == 0) {
                                GCAppSettings.APN = apnField.getText();
                                GCAppSettings.APN_Username = usernameField.getText();
                                GCAppSettings.APN_Password = passwordField.getText();
                            }
                            apnDialogueShown = true;
                        }
                    }
                });
                String url = baseURL + ";deviceside=true";
                String apn = GCAppSettings.APN;//""; //inputs.getEfTcpAPN();
                String username = GCAppSettings.APN_Username; //inputs.getEfTcpAPNUser();
                String password = GCAppSettings.APN_Password;//""; //inputs.getEfTcpAPNPassword();
                if (apn.length() > 0) {
                    url += ";apn=" + apn;
                }
                if (username.length() > 0) {
                    url += ";TunnelAuthUsername=" + username;
                }
                if (password.length() > 0) {
                    url += ";TunnelAuthPassword=" + password;
                }
                return url;
            }
        
            /**
             * Constructs a MDS url from the baseURL provided by the user
             * @return A url with MDS parameters
             */
            private String getMDSURL(String baseURL) {
                return baseURL + ";deviceside=false";
            }
        
            /**
             * Constructs a Unite url from the baseURL provided by the user
             * @return A url with Unite parameters
             */
            private String getUniteURL(String baseURL) {
                return baseURL + ";deviceside=false" + ";ConnectionUID=" + srUnite.getUid();
            }
        
            /**
             * Constructs a BIS-B url from the baseURL provided by the user
             * @return A url with BIS-B parameters
             */
            private String getBISURL(String baseURL) {
                return baseURL + ";deviceside=false"; // Not implemented since this is only available to ISV partners of RIM
            }
        
            /**
             * Constructs a WAP2.0 url from the baseURL provided by the user
             * @return A url with WAP2.0 parameters
             */
            private String getWAP2URL(String baseURL) {
                return baseURL + ";deviceside=true" + ";ConnectionUID=" + srWAP2.getUid();
            }
        
            /**
             * Constructs a WiFi url from the baseURL provided by the user
             * @return A url with WiFi parameters
             */
            private String getWiFiURL(String baseURL) {
                return baseURL + ";interface=wifi";
            }
        
            /**
             * Constructs a WAP1.0 url from the baseURL provided by the user
             * @return A url with WAP1.0 parameters
             */
            private String getWAPURL(String baseURL) {
                String url = baseURL + ";deviceside=true";
                String gatewayIP = ""; //inputs.getEfWapGatewayIP();
                String gatewayAPN = ""; //inputs.getEfWapGatewayAPN();
                String gatewayPort = ""; //inputs.getEfWapGatewayPort();
                String sourceIP = ""; //inputs.getEfWapSourceIP();
                String sourcePort = ""; //inputs.getEfWapSourcePort();
                String username = ""; //inputs.getEfWapUser();
                String password = ""; //inputs.getEfWapPassword();
                if (gatewayIP.length() > 0) {
                    url = url + ";WapGatewayIP=" + gatewayIP;
                    wapParametersUnavailable = false;
                }
                if (gatewayAPN.length() > 0) {
                    url = url + ";WapGatewayAPN=" + gatewayAPN;
                    wapParametersUnavailable = false;
                }
                if (gatewayPort.length() > 0) {
                    url = url + ";WapGatewayPort=" + gatewayPort;
                    wapParametersUnavailable = false;
                }
                if (sourceIP.length() > 0) {
                    url = url + ";WapSourceIP=" + sourceIP;
                    wapParametersUnavailable = false;
                }
                if (sourcePort.length() > 0) {
                    url = url + ";WapSourcePort=" + sourcePort;
                    wapParametersUnavailable = false;
                }
                if (username.length() > 0) {
                    url = url + ";TunnelAuthUsername=" + username;
                    wapParametersUnavailable = false;
                }
                if (password.length() > 0) {
                    url = url + ";TunnelAuthPassword=" + password;
                    wapParametersUnavailable = false;
                }
        
                /*if (inputs.getCfWapEnableWTLS()) {
                    url = url + ";WapEnableWTLS=true";
                    wapParametersUnavailable = false;
                }*/
                if (wapParametersUnavailable && srWAP != null) {
                    return url;
                }/** Not implemented */
                else {
                    return url;
                }
            }
        

        【讨论】:

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