HTTP POST与黑莓6.0的问题 [英] Http Post with Blackberry 6.0 issue
问题描述
我想一些数据发布到web服务我们(C#编写的),并获得响应。响应是JSON格式。
我使用的是黑莓code样品是BlockingSenderDestination样品。当我请求一个页面就没有问题返回。但是,当我把我的数据,我们的web服务不会返回任何东西。
这是我添加的code部分是:
ByteMessage myMsg = bsd.createByteMessage();
//myMsg.setStringPayload(\"I爱我的BlackBerry设备)!;
myMsg.setMessageProperty(querytpe,myspecialkey); //这里是我的职务数据
myMsg.setMessageProperty(UNAME,名为myusername);
myMsg.setMessageProperty(通,密码);
((HttpMessage)myMsg).setMethod(HttpMessage.POST);
//发送消息并等待响应myMsg
响应= bsd.sendReceive(myMsg);
我究竟做错了什么?什么是替代品以上efficients的方式做邮政与黑莓。
问候。
下面是我的整个code:
类BlockingSenderSample扩展MainScreen实现FieldChangeListener {ButtonField字段_btnBlock =新ButtonField字段(Field.FIELD_HCENTER);
私有静态的UIApplication _app = UiApplication.getUiApplication();
私人字符串_result;
公共BlockingSenderSample()
{
_btnBlock.setChangeListener(本);
_btnBlock.setLabel(抓取页面);
加(_btnBlock);
}公共无效fieldChanged(字段按钮,诠释未使用)
{ 如果(按钮== _btnBlock)
{ 线程t =新主题(新的Runnable()
{
公共无效的run()
{
消息响应= NULL;
字符串uriStr =http://192.168.1.250/mobileServiceOrjinal.aspx; //我们的Web服务地址
//字符串uriStr =http://www.blackberry.com; BlockingSenderDestination BSD = NULL;
尝试
{
BSD =(BlockingSenderDestination)
DestinationFactory.getSenderDestination
(名,URI.create(uriStr)); //名称上下文的名字。是真的吗?
如果(BSD == NULL)
{
BSD =
DestinationFactory.createBlockingSenderDestination
(新上下文(安德),
URI.create(uriStr)
);
}
//Dialog.inform(1);
ByteMessage myMsg = bsd.createByteMessage();
//myMsg.setStringPayload(\"I爱我的BlackBerry设备)!;
myMsg.setMessageProperty(querytpe,myspecialkey); //这里是我的职务数据
myMsg.setMessageProperty(UNAME,名为myusername);
myMsg.setMessageProperty(通,密码);
((HttpMessage)myMsg).setMethod(HttpMessage.POST); //发送消息并等待响应myMsg
响应= bsd.sendReceive(myMsg); 如果(响应!= NULL)
{
BSDResponse(响应);
}
}
赶上(例外五)
{
//Dialog.inform(EX);
//过程中的误差
}
最后
{
如果(BSD!= NULL)
{
bsd.release();
}
}
} });
t.start(); }
}私人无效BSDResponse(消息MSG)
{
如果(MSG的instanceof ByteMessage)
{
ByteMessage回复=(ByteMessage)味精;
_result =(字符串)reply.getStringPayload();
}否则如果(MSG的instanceof StreamMessage)
{
StreamMessage回复=(StreamMessage)味精;
InputStream为= reply.getStreamPayload();
字节[]数据= NULL;
尝试{
数据= net.rim.device.api.io.IOUtilities.streamToBytes(是);
}赶上(IOException异常五){
//过程中的误差
}
如果(数据!= NULL)
{
_result =新的String(数据);
}
} _app.invokeLater(新的Runnable(){ 公共无效的run(){
_app.pushScreen(新HTTPOutputScreen(_result));
} });}}
..
类HTTPOutputScreen扩展MainScreen
{RichTextField _rtfOutput =新RichTextField();公共HTTPOutputScreen(字符串消息)
{
_rtfOutput.setText(检索数据请稍候...);
加(_rtfOutput);
showContents(消息);
}该数据被检索//后,显示它
公共无效showContents(最后弦乐结果)
{
UiApplication.getUiApplication()的invokeLater(Runnable的新()
{ 公共无效的run()
{
_rtfOutput.setText(结果);
}
});
}
}
HttpMessage没有延伸ByteMessage所以当你做的:
((HttpMessage)myMsg).setMethod(HttpMessage.POST);
它抛出一个ClassCastException。这里是我会做什么,而不是一个大致的轮廓。请注意,这仅仅是例子code,我忽略异常和等。
//注:URL需要有相应的连接设置附加
的HttpConnection httpConn =(HttpConnection的)Connector.open(URL);
httpConn.setRequestMethod(HttpConnection.POST);
OutputStream的OUT = httpConn.openOutputStream();
out.write(lt;您的数据HERE>);
了out.flush();
out.close();InputStream的时间= httpConn.openInputStream();
//读取输入流中,如果你想从服务器的响应如果(httpConn.getResponse code()!= HttpConnection.OK)
{
//做的错误处理。
}
I am trying to post some data to our webservice(written in c#) and get the response. The response is in JSON format.
I am using the Blackberry Code Sample which is BlockingSenderDestination Sample. When I request a page it returns with no problem. But when I send my data to our webservice it does not return anything.
The code part that I added is :
ByteMessage myMsg = bsd.createByteMessage();
//myMsg.setStringPayload("I love my BlackBerry device!");
myMsg.setMessageProperty("querytpe","myspecialkey");//here is my post data
myMsg.setMessageProperty("uname","myusername");
myMsg.setMessageProperty("pass","password");
((HttpMessage) myMsg).setMethod(HttpMessage.POST);
// Send message and wait for response myMsg
response = bsd.sendReceive(myMsg);
What am i doing wrong? And what is the alternatives or more efficients way to do Post with Blackberry.
Regards.
Here is my whole code:
class BlockingSenderSample extends MainScreen implements FieldChangeListener {
ButtonField _btnBlock = new ButtonField(Field.FIELD_HCENTER);
private static UiApplication _app = UiApplication.getUiApplication();
private String _result;
public BlockingSenderSample()
{
_btnBlock.setChangeListener(this);
_btnBlock.setLabel("Fetch page");
add(_btnBlock);
}
public void fieldChanged(Field button, int unused)
{
if(button == _btnBlock)
{
Thread t = new Thread(new Runnable()
{
public void run()
{
Message response = null;
String uriStr = "http://192.168.1.250/mobileServiceOrjinal.aspx"; //our webservice address
//String uriStr = "http://www.blackberry.com";
BlockingSenderDestination bsd = null;
try
{
bsd = (BlockingSenderDestination)
DestinationFactory.getSenderDestination
("name", URI.create(uriStr));//name for context is name. is it true?
if(bsd == null)
{
bsd =
DestinationFactory.createBlockingSenderDestination
(new Context("ender"),
URI.create(uriStr)
);
}
//Dialog.inform( "1" );
ByteMessage myMsg = bsd.createByteMessage();
//myMsg.setStringPayload("I love my BlackBerry device!");
myMsg.setMessageProperty("querytpe","myspecialkey");//here is my post data
myMsg.setMessageProperty("uname","myusername");
myMsg.setMessageProperty("pass","password");
((HttpMessage) myMsg).setMethod(HttpMessage.POST);
// Send message and wait for response myMsg
response = bsd.sendReceive(myMsg);
if(response != null)
{
BSDResponse(response);
}
}
catch(Exception e)
{
//Dialog.inform( "ex" );
// process the error
}
finally
{
if(bsd != null)
{
bsd.release();
}
}
}
});
t.start();
}
}
private void BSDResponse(Message msg)
{
if (msg instanceof ByteMessage)
{
ByteMessage reply = (ByteMessage) msg;
_result = (String) reply.getStringPayload();
} else if(msg instanceof StreamMessage)
{
StreamMessage reply = (StreamMessage) msg;
InputStream is = reply.getStreamPayload();
byte[] data = null;
try {
data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
} catch (IOException e) {
// process the error
}
if(data != null)
{
_result = new String(data);
}
}
_app.invokeLater(new Runnable() {
public void run() {
_app.pushScreen(new HTTPOutputScreen(_result));
}
});
}
}
..
class HTTPOutputScreen extends MainScreen
{
RichTextField _rtfOutput = new RichTextField();
public HTTPOutputScreen(String message)
{
_rtfOutput.setText("Retrieving data. Please wait...");
add(_rtfOutput);
showContents(message);
}
// After the data has been retrieved, display it
public void showContents(final String result)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
_rtfOutput.setText(result);
}
});
}
}
HttpMessage does not extend ByteMessage so when you do:
((HttpMessage) myMsg).setMethod(HttpMessage.POST);
it throws a ClassCastException. Here's a rough outline of what I would do instead. Note that this is just example code, I'm ignoring exceptions and such.
//Note: the URL will need to be appended with appropriate connection settings
HttpConnection httpConn = (HttpConnection) Connector.open(url);
httpConn.setRequestMethod(HttpConnection.POST);
OutputStream out = httpConn.openOutputStream();
out.write(<YOUR DATA HERE>);
out.flush();
out.close();
InputStream in = httpConn.openInputStream();
//Read in the input stream if you want to get the response from the server
if(httpConn.getResponseCode() != HttpConnection.OK)
{
//Do error handling here.
}
这篇关于HTTP POST与黑莓6.0的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!