BlackBerry模拟器可以连接到Web服务,但实际的设备不能 [英] BlackBerry simulator can connect to web service, but real device can't

查看:253
本文介绍了BlackBerry模拟器可以连接到Web服务,但实际的设备不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个连接到Web服务的BlackBerry应用程序。

I am developing BlackBerry application that connects to the web service.

当我在模拟器开发的,我用黑莓MDS的模拟器,一切都只是去罚款。我的应用程序(在模拟器上运行)可以用我的web服务完美连接。请注意,模拟器和web服务都在不同的PC。

When I developed on simulator, I used BlackBerry MDS for simulator and everything just went fine. My application (running on simulator) can connect with my web service perfectly. Please note that the simulator and the web service are on different PCs.

现在,我的项目已经完成。我试图部署我的应用程序到真正的设备(BB 8520)。当我使用的设备上的应用程序,我发现它无法连接到Web服务。我没有在互联网上的研究,我相信它一定是由于MDS的问题。这似乎是我必须做的与MDS东西在我的Web服务所在的计算机上,但我仍然找不到明显的答案。

Now, my project is done. I tried deploying my app to the real device (BB 8520). When I used the app on the device, I found it can't connect to the web service. I did a research on the Internet and I am sure it must be due to MDS issue. It seems like I have to do something with MDS on the computer where my web service resides, but I still can't find the obvious answer.

请人帮我...

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

PS. My web service is published on IIS and is developed in Visual Studio 2010. The BlackBerry application is developed in Eclipse and connects to web service via ksoap2. Firewall on the computer where web service resides is closed. The connection used is WIFI.

推荐答案

您必须对所有连接参数加入到使用URL HTTPConnetion 。创建一个自定义类检查引黄联接参数。使用下面的自定义类

You have to add all connection parameter into your URL using HTTPConnetion. Create one Custom class to check connetion parameter .. USE the below 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());
}
}

以上级整合后u必须做一个包的名称 impl.javame.com.twitterapime.io 并添加下面的类。

`包impl.javame.com.twitterapime.io;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口javax.microedition.io.Connector;
进口com.twitterapime.io.HttpConnection;

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

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

public class HttpConnectionImpl implements HttpConnection { private 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);       
}

}`

现在,你必须整合另外两个班

Now you have to integrate another two class


  1. 的HttpConnection

  2. 的Htt presponsein com.twitterapime.io 包。

  1. HttpConnection
  2. HttpResponsein com.twitterapime.io package.

`包com.twitterapime.io;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
公共接口的HttpConnection {
    公共静态最后弦乐GET =GET;

`package com.twitterapime.io; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public interface 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是Htt的presponse Class`

Bwlow is HttpResponse Class`

`包com.twitterapime.io;

`package com.twitterapime.io;

进口java.io.ByteArrayOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.UnsupportedEncodingException;

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException;

公共final类的Htt $ P $ {psponse
    私人诠释code;

public final class HttpResponse { private int code;

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

}`

使用下面的马托检查您的引黄联接参数。此方法会自动检查你的设备引黄联接的可用性,并添加引黄联接参数,根据您的连接。

Use the below mathod to check your connetion parameter. This method automatically check your device connetion availability and add connetion parameter based on your connection.

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

以上的HttpConnection 与引黄联接参数方法的返回URL ..妳比在YOUT使用引黄联接的InputStream 和打开任何URL以及web服务。

The above HttpConnection method return URL with connetion parameter .. than u can use this connetion in yout InputStream and open any url as well as webservice.

我希望这将帮助你...

i hope this will help you...

这篇关于BlackBerry模拟器可以连接到Web服务,但实际的设备不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆