简体   繁体   English

将BlackBerry Httpconnection发布到.Net Web服务无法在设备上运行

[英]BlackBerry Httpconnection post to a .Net Webservice not working on device

I am developing an application to use httpconnection to get some data to a .Net Webservice. 我正在开发一个使用httpconnection将一些数据获取到.Net Webservice的应用程序。 The code is working fine when i host the webservice locally and test it with Eclipse simulator, but when i host the webservice on the public deployment server and package the Blackberry app to my device all am getting is is a respose code 500. Please can someone give me a clue, an answer or a solution to what migth be causing this? 当我在本地托管Webservice并使用Eclipse模拟器对其进行测试时,该代码工作正常,但是当我在公共部署服务器上托管webservice并将Blackberry应用程序打包到我的设备上时,得到的是一个重新放置代码500。请有人给我一个线索,答案或解决方案,以解决造成此问题的原因?

Below are the code i used: 以下是我使用的代码:

String URL = WebServiceURL+"/Login"+getConnectionParameter();

public static String getConnectionParameter() {
    String ConnectionParameter ="" ;
    if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
        // Connected to a WiFi access point
        ConnectionParameter = ";interface=wifi";
    } else {
        int coverageStatus = CoverageInfo.getCoverageStatus();
        ServiceRecord record = getWAP2ServiceRecord();
        if (record != null && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
            // Have network coverage and a WAP 2.0 service book record
            ConnectionParameter = ";deviceside=true;ConnectionUID="+ record.getUid();
        } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
            // Have an MDS service book and network coverage
            ConnectionParameter = ";deviceside=false";
        } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
            // Have network coverage but no WAP 2.0 service book record
            ConnectionParameter = ";deviceside=true";
        }
    }
    return ConnectionParameter;
}

URLEncodedPostData oPostData = new URLEncodedPostData(
                URLEncodedPostData.DEFAULT_CHARSET, false);
                oPostData.append("username",username.getText());
                oPostData.append("compressedpicture",HomeScreen.convertByteArrayToString(imageArray));
                oPostData.append("email",email.getText());
                oPostData.append("password",password.getText());
                oPostData.append("pin",pin.getText());
                oPostData.append("extension",extension);
                String URL = MYAPP.WebServiceURL+"/SignUp"+MYAPP.getConnectionParameter();
                String result = HomeScreen.postinfo(URL,oPostData);
                Dialog.inform(result);
                if(result.indexOf("success")!= -1){
                    Dialog.inform("You Have just sign up. Congratulations.");
                }else{
                    Dialog.inform("There is an issue registering you"); 
                }




public static String postinfo(String _URL,URLEncodedPostData _PostData) {
    String result = "";
    try {
        HttpConnection conn = (HttpConnection) Connector.open(_URL,
                Connector.READ_WRITE);
        if (_PostData != null) {
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",
                    Integer.toString(_PostData.size()));
            OutputStream strmOut = conn.openOutputStream();
            strmOut.write(_PostData.getBytes());
            strmOut.close();
        } else {
            conn.setRequestMethod(HttpConnection.GET);
        }

        int responseCode = conn.getResponseCode();
        if (responseCode == HttpConnection.HTTP_OK) {
            InputStream data = conn.openInputStream();
            StringBuffer raw = new StringBuffer();
            byte[] buf = new byte[4096];
            int nRead = data.read(buf);
            while (nRead > 0) {
                raw.append(new String(buf, 0, nRead));
                nRead = data.read(buf);
            }
            result = raw.toString();
            //Dialog.alert("Result:" + raw.toString());
            //_Dest.updateDestination(raw.toString());
        } else {

            _Dest.updateDestination("responseCode="
                    + Integer.toString(responseCode));
            result = "responseCode= "+ Integer.toString(responseCode);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        //_Dest.updateDestination("Exception:" + e.toString());
        result = "Exception:" + e.toString();
    }
    return result;
}

First thing, don't use the flags like that because they can give odd results sometimes. 首先,不要使用这样的标志,因为它们有时会给出奇怪的结果。 The way to check a connection type is through the CoverageInfo class: 检查连接类型的方法是通过CoverageInfo类:

CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B);
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
//...

Second, I think you sould check that the URL suffix (named ConnectionParameter in your code, BTW this name doesn't follow Java Naming Conventions) is initialized correctly when on device, because I think you have missed a few options (like BIS and other rarely-used WAP and APN options). 其次,我认为您应该检查在设备上安装时是否正确初始化了URL后缀(在代码中命名为ConnectionParameter ,此名称不遵循Java命名约定),因为我认为您错过了一些选项(例如BIS和其他很少使用的WAP和APN选项)。 That may give you a hint of where the problem is. 这可能会提示您问题所在。

To check that there's no problem with your code, you can type in the URL in the BlackBerry browser to see if the result is the same. 要检查代码是否没有问题,可以在BlackBerry浏览器中键入URL,以查看结果是否相同。

Finally, the HTTP 500 is a Server Error, so the problem may have to do with the server as well. 最后,HTTP 500是服务器错误,因此问题也可能与服务器有关。

UPDATE: Unless you are programming for old devices, you should use the ConnectionFactory class that handles the URLs and suffixes for you. 更新:除非您为旧设备编程,否则应使用为您处理URL和后缀的ConnectionFactory类。 It was added on OS 5.0. 它是在OS 5.0上添加的。

Thanks for all your suggestion Guys. 谢谢您的所有建议。 I appreciate them all. 我很感谢他们。 I discovered my production server was throwing the internal server error 500 because of of the /WebserviceMethod at end of my URL eg 我发现生产服务器由于URL末尾的/ WebserviceMethod而引发内部服务器错误500,例如

String URL = WebServiceURL+"**/Login**"+getConnectionParameter();

I found a solution using KSOAP2. 我找到了使用KSOAP2的解决方案。 It worked perfectly well. 它工作得很好。 Though u might av to write some lines of code to handle the parsed response format(KSOAP2 return a parsed format of the response XML). 尽管您可能会编写一些代码行来处理解析后的响应格式(KSOAP2返回了响应XML的解析格式)。

Am sure you will find the article at this link useful: KSoap Android Web Service Tutorial With Sample Code 确保该链接上的文章对您有用: KSoap Android Web Service Tutorial with Sample Code

It applies to most J2ME based mobile platform. 它适用于大多数基于J2ME的移动平台。

Once again Thank you all for your time and help. 再次感谢大家的时间和帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM