2011-09-23 138 views
1

我想将一些数据发布到我们的webservice(用c#编写)并获得响应。响应采用JSON格式。Http Post with Blackberry 6.0问题

我使用的是黑莓代码示例是BlockingSenderDestination样品。当我请求一个页面时,它返回没有问题。但是,当我将数据发送到我们的web服务时,它不会返回任何内容。

我添加的代码部分是:

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); 

我到底做错了什么?什么是替代品或更有效的方式来做与黑莓Post。

问候。

这里是我的全部代码:

((HttpMessage) myMsg).setMethod(HttpMessage.POST); 

它抛出一个ClassCastException:

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); 
     } 
    }); 
} 
} 

回答

2

HttpMessage当你不延长ByteMessage如此。以下是我将要做的事情的大致轮廓。请注意,这只是示例代码,我忽略了异常等。

//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. 
} 
+0

问题是我应该在“out.write(MYDATA)”中写什么?例如,我应该写我的uname和值写方法的格式,以便服务器可以理解。 – henderunal

+0

这个问题的答案实际上取决于服务器期望的格式。输出流需要一个字节数组,所以你可以写任何你想要的数据。不过,我怀疑你想发送URL编码数据,在这种情况下,你应该使用URLEncodedPostData类。要获取输出流期望的字节数组,请使用urlEncodedData.toString()。getBytes(“UTF-8”); – Jonathan

+0

正如你所说的服务器发送响应在JSON所以有机会在那里,服务器也期待在JSON的请求...给它尝试... –