2011-09-23 2 views
1

우리 웹 서비스 (C#으로 작성)에 일부 데이터를 게시하고 응답을 얻으려고합니다. 응답은 JSON 형식입니다.Blackberry 6.0 문제가있는 Http Post

BlockingSenderDestination Sample 인 Blackberry Code Sample을 사용하고 있습니다. 페이지를 요청할 때 문제없이 반환됩니다. 그러나 웹 서비스에 데이터를 보내면 아무 것도 반환하지 않습니다. 내가 추가

코드 부분은 : 내가 잘못 뭐하는 거지

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

? 그리고 Blackberry로 Post를하는 대안이나 더 효율적인 방법은 무엇입니까?

감사합니다. 이 ClassCastException를 슬로우

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

: 당신이 할 때

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과 value를 작성하여 메소드를 작성해야합니다. – henderunal

+0

그 질문에 대한 대답은 실제로 서버가 기대하는 형식에 달려 있습니다. 출력 스트림은 바이트 배열을 사용하므로 원하는 모든 데이터를 쓸 수 있습니다. 그러나 URL 인코딩 된 데이터를 보내려는 경우에는 URLEncodedPostData 클래스를 사용해야합니다. 출력 스트림에서 예상하는 바이트 배열을 가져 오려면 urlEncodedData.toString(). getBytes ("UTF-8"); – Jonathan

+0

당신이 말했듯이 서버가 json에서 응답을 보내면 서버가 Json에서 요청을 기대할 가능성이 있습니다 ... 시도해보십시오 ... –