我正在开发一个应用程序来使用httpconnection将一些数据传递给.Net Webservice。当我在本地托管web服务并使用Eclipse模拟器测试它时,代码工作正常,但是当我在公共部署服务器上托管webservice并将Blackberry应用程序打包到我的设备时,我得到的是一个respose代码500.请有人可以给我一个线索,答案或解决什么是migth造成这种情况?
以下是我使用的代码:
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;
}
答案 0 :(得分:0)
首先,不要使用那样的标志,因为它们有时会给出奇怪的结果。检查连接类型的方法是通过CoverageInfo
类:
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B);
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
//...
其次,我认为您应该检查在设备上是否正确初始化了URL后缀(代码中名为ConnectionParameter
,BTW此名称不遵循Java命名约定),因为我认为您错过了几个选项(如BIS和其他很少使用的WAP和APN选项)。这可能会给你一个问题所在的暗示。
要检查代码是否有问题,您可以在BlackBerry浏览器中输入URL以查看结果是否相同。
最后,HTTP 500是服务器错误,所以问题也可能与服务器有关。
更新:除非您使用旧设备进行编程,否则应使用为您处理URL和后缀的ConnectionFactory
类。它已在OS 5.0上添加。
答案 1 :(得分:0)
感谢您的所有建议伙计们。我很欣赏他们。我发现我的生产服务器因为我的URL末尾的/ WebserviceMethod而抛出了内部服务器错误500,例如。
String URL = WebServiceURL+"**/Login**"+getConnectionParameter();
我找到了使用KSOAP2的解决方案。它工作得很好。虽然你可能会编写一些代码来处理解析的响应格式(KSOAP2返回一个解析后的格式的响应XML)。
我相信您会发现此链接上的文章很有用: KSoap Android Web Service Tutorial With Sample Code
它适用于大多数基于J2ME的移动平台。
再次感谢大家的时间和帮助。