0

I am developing an application to use httpconnection to get some data to a .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?

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;
}
Matteo
  • 14,696
  • 9
  • 68
  • 106
Isola Olufemi
  • 23
  • 1
  • 8
  • You have server response, so this means the BB networking works ok. I'd suggest to check server logs for the issue reason. – Vit Khudenko Nov 28 '11 at 17:31
  • I got the following log on my IIS server. Idont know what it means :2011-11-29 16:13:41 myIP POST /myapp/WebService1.asmx/Login - 80 - IP - 500 0 0 218 – Isola Olufemi Nov 29 '11 at 16:38
  • This does not tell much except that the server got your POST request to the stated url, port and from the stated IP. – Vit Khudenko Nov 29 '11 at 17:30

2 Answers2

0

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.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). 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.

Finally, the HTTP 500 is a Server Error, so the problem may have to do with the server as well.

UPDATE: Unless you are programming for old devices, you should use the ConnectionFactory class that handles the URLs and suffixes for you. It was added on OS 5.0.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • The ConnectionParameter method is giving a good return value and appending it to the connection string as a suffix. it worked well when i test it locally using a simulator and a locally deployed webservice. The problem is that is not working on my device.When tested the webservice from my BB browser i got the correct webservice page. Is still need more suggestions. – Isola Olufemi Nov 28 '11 at 12:47
  • Ok, so the problem is in your app, since the server is reachable from the device. Then check the suffix value debugging on device. – Mister Smith Nov 28 '11 at 12:54
  • I got the following log on my IIS server:2011-11-29 16:13:41 myIP POST /myapp/WebService1.asmx/Login - 80 - IP - 500 0 0 218 – Isola Olufemi Nov 29 '11 at 16:37
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 e.g.

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

I found a solution using 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).

Am sure you will find the article at this link useful: KSoap Android Web Service Tutorial With Sample Code

It applies to most J2ME based mobile platform.

Once again Thank you all for your time and help.

Isola Olufemi
  • 23
  • 1
  • 8