Blackberry发送HTTPPost请求
我正在为blackberry开发应用程序,我需要向我的服务器发送Http Post请求。我正在使用模拟器测试我的应用程序,我发现以下代码用于发送请求: 但我无法让它工作,因为它在这方面失败了:Blackberry发送HTTPPost请求,blackberry,Blackberry,我正在为blackberry开发应用程序,我需要向我的服务器发送Http Post请求。我正在使用模拟器测试我的应用程序,我发现以下代码用于发送请求: 但我无法让它工作,因为它在这方面失败了: int rc = _httpConnection.getResponseCode(); 有什么想法吗 谢谢我不确定您发布的站点,但我已经成功使用了blackberry站点上提供的示例ConnectionFactory代码 请确保不要在EventThread上调用连接。以下是关于如何发送POST请求的示例
int rc = _httpConnection.getResponseCode();
有什么想法吗
谢谢我不确定您发布的站点,但我已经成功使用了blackberry站点上提供的示例ConnectionFactory代码
请确保不要在EventThread上调用连接。以下是关于如何发送POST请求的示例代码:
HttpConnection c = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
c.setRequestMethod(HttpConnection.POST);
OutputStream os = c.openOutputStream();
os.write(request.getBytes("UTF-8"));
os.flush();
os.close();
InputStream is = c.openInputStream();
请确保在单独的线程中使用此代码。我知道这个问题很老,OP可能已经解决了,但我刚刚遇到了同样的问题并设法解决了它
public static ResponseBean sendRequestAndReceiveResponse(String method, String absoluteURL, String bodyData, boolean readResponseBody)
throws IOException
{
ResponseBean responseBean = new ResponseBean();
HttpConnection httpConnection = null;
try
{
String formattedURL = absoluteURL + "deviceside=true;interface=wifi"; // If you are using WiFi
//String formattedURL = absoluteURL + "deviceside=false"; // If you are using BES
//String formattedURL = absoluteURL + "deviceside=true"; // If you are using TCP
if(DeviceInfo.isSimulator()) // if you are using simulator
formattedURL = absoluteURL;
httpConnection = (HttpConnection) Connector.open(formattedURL);
httpConnection.setRequestMethod(method);
if (bodyData != null && bodyData.length() > 0)
{
OutputStream os = httpConnection.openOutputStream();
os.write(bodyData.getBytes("UTF-8"));
}
int responseCode = httpConnection.getResponseCode();
responseBean.setResponseCode(responseCode);
if (readResponseBody)
{
responseBean.setBodyData(readBodyData(httpConnection));
}
}
catch (IOException ex)
{
System.out.println("!!!!!!!!!!!!!!! IOException in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
throw ex;
}
catch(Exception ex)
{
System.out.println("!!!!!!!!!!!!!!! Exception in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
throw new IOException(ex.toString());
}
finally
{
if (httpConnection != null)
httpConnection.close();
}
return responseBean;
}
public static StringBuffer readBodyData(HttpConnection httpConnection) throws UnsupportedEncodingException, IOException
{
if(httpConnection == null)
return null;
StringBuffer bodyData = new StringBuffer(256);
InputStream inputStream = httpConnection.openDataInputStream();
byte[] data = new byte[256];
int len = 0;
int size = 0;
while ( -1 != (len = inputStream.read(data)) )
{
bodyData.append(new String(data, 0, len,"UTF-8"));
size += len;
}
if (inputStream != null)
{
inputStream.close();
}
return bodyData;
}
你需要附加;deviceside=符合您的URL
例如,您的URL将从http://example.com/directory/submitpost.php 到http://example.com/directory/submitpost.php;deviceside=true
我在这里找到了这个:
我的POST请求在3分钟后超时,当时我没有看到这个See,但是它可以很好地将这个附加到url中
我还建议使用ConnectionFactory。以下是我的一些代码:
Network.httpPosthttp://example.com/directory/submitpost.php;deviceside=true,paramNamesArray,paramValsArray
公共静态无效httpPostString urlStr,字符串[]paramName,字符串[]paramVal引发异常{
ConnectionFactory conFactory=新的ConnectionFactory;
conFactory.setTimeLimit1000;
HttpConnection conn=HttpConnection conFactory.getConnectionurlStr.getConnection;
conn.setRequestMethodHttpConnection.POST;
conn.setRequestPropertyContent-Type,application/x-www-form-urlencoded;
StringBuffer sb=新的StringBuffer;
对于int i=0;iStringBuffer postData = new StringBuffer();
httpConn = (HttpConnection) Connector.open("https://surveys2.kenexa.com/feedbacksurveyapi/login?");
httpConn.setRequestMethod(HttpConnection.POST);
postData.append("username="+username);
postData.append("&password="+pass);
postData.append("&projectcode="+projectid);
String encodedData = postData.toString();
httpConn.setRequestProperty("Content-Language", "en-US");
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString());
byte[] postDataByte = postData.toString().getBytes("UTF-8");
OutputStream out = httpConn.openOutputStream();
out.write(postDataByte);
out.close();
httpConn.getResponseCode();
您得到的错误是什么?在这条线上发生了什么?是的,发生了什么?另外,您是在事件线程上调用postData方法,还是启动一个单独的线程?事件线程上的HTTP访问将导致问题。也会遇到此问题。线程非UI在httpConn.getResponseCode处等待;然后退出一段时间,出现异常:java.io.InterruptedIOException:本地连接在120000之后超时。可以从模拟器上的浏览器访问服务器。我使用的代码与下面的答案类似@xger86x您找到这个方法了吗?请确保在单独的线程中调用此方法,并且在更新UI之前,必须使用Application.getUiApplication.getEventLock获取eventLock;如何设置POST参数?