2011-09-19 169 views
0

我想通過使用URLEncodedPost類發送數據到服務器。 我正在嘗試調用POST方法時出現錯誤。所以如果有人對這種方法有任何的想法,那就給我一些提示。黑莓服務器連接問題

enter code here 
+1

發佈您的代碼來解決問題 – koti

回答

0

您還沒有發佈任何代碼,知道你做這錯誤的任何方式,下面的代碼是HTTP POST方法的一個例子

HttpConnection connection = (HttpConnection) Connector.open("url", Connector.READ_WRITE); 
connection.setRequestMethod(HttpConnection.POST); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false); 
encPostData.append("username","your username"); 
encPostData.append("password","ur password"); 
byte[] postData = encPostData.toString().getBytes("UTF-8"); 
connection.setRequestProperty("Content-Length", String.valueOf(postData.length)); 
OutputStream os = connection.openOutputStream(); 
os.write(postData); 
os.flush(); 
int responseCode = connection.getResponseCode(); 
if (responseCode != HttpConnection.HTTP_OK) 
{ 
System.out.println("Unexpected response code: "+ responseCode); 
connection.close(); 
return; 
} 
String contentType = connection.getHeaderField("Content-type"); 
baos = new ByteArrayOutputStream(); 
InputStream responseData = connection.openInputStream(); 
byte[] buffer = new byte[10000]; 
int bytesRead = responseData.read(buffer); 
while (bytesRead > 0) 
{ 
baos.write(buffer, 0, bytesRead); 
bytesRead = responseData.read(buffer); 
} 
baos.close(); 
connection.close(); 
System.out.println("Server response"+new String(baos.toByteArray()));