我正在开发一个应用程序,使用httpconnection来获取一些数据到。net Webservice。当我在本地托管web服务并使用Eclipse模拟器进行测试时,代码工作正常,但是当我在公共部署服务器上托管web服务并将黑莓应用程序打包到我的设备时,我得到的只是响应代码500。请有人给我一个线索,一个答案或一个解决方案,什么可能会导致这种情况?
下面是我使用的代码: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;
}
首先,不要使用这样的标志,因为它们有时会给出奇怪的结果。检查连接类型的方法是通过CoverageInfo
类:
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B);
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
//...
第二,我认为你应该检查URL后缀(在你的代码中命名为ConnectionParameter
,顺便说一下这个名字不遵循Java命名约定)在设备上正确初始化,因为我认为你错过了一些选项(如BIS和其他很少使用的WAP和APN选项)。这可能会给你一个提示,让你知道问题在哪里。
要检查代码是否有问题,你可以在黑莓浏览器中输入URL,看看结果是否相同。
最后,HTTP 500是一个服务器错误,所以问题也可能与服务器有关。
更新:除非你是为旧设备编程,你应该使用ConnectionFactory
类来为你处理url和后缀。
谢谢大家的建议。我很感激他们。我发现我的生产服务器抛出内部服务器错误500,因为/WebserviceMethod在我的URL的末尾,例如
String URL = WebServiceURL+"**/Login**"+getConnectionParameter();
我找到了一个使用KSOAP2的解决方案。它运行得非常好。尽管您可能需要编写一些代码行来处理解析后的响应格式(KSOAP2返回解析后的响应XML格式)。
我相信你会发现这个链接的文章很有用:KSoap Android Web Service教程与示例代码
适用于大多数基于J2ME的移动平台。
再次感谢大家的时间和帮助。