2011-09-01 52 views
-1

我已经粘贴了一个HTTP Post的代码片段,用于将多部分消息发送到需要身份验证的服务器。我期待JSON响应,但是当我运行这个时,我总是得到HTML中的登录页面。HTTP POST不会返回预期的JSON响应

public final class MyScreen extends MainScreen { 
private RichTextField _Output; 
public MyScreen() { 
// Set the displayed title of the screen 
setTitle("MyTitle"); 
_Output = new RichTextField(); 
add(_Output); 
addMenuItem(_GetDataAction); 
} 


protected MenuItem _GetDataAction = new MenuItem("GetData", 100000, 10) { 
public void run() { 
    String URL = "<Sample URL Goes Here>"; 
    ServiceRequestThread svc = new ServiceRequestThread(URL, 
    (MyScreen) UiApplication.getUiApplication() 
    .getActiveScreen()); 
    svc.start(); 
    } 
}; 


    public void updateDestination(final String text) { 
    UiApplication.getUiApplication().invokeLater(new Runnable() { 
    public void run() { 
    _Output.setText(text); 
    } 
    }); 
    } 
    } 


    class ServiceRequestThread extends Thread { 
    protected String _URL; 
    protected MyScreen _Dest = null; 
    protected URLEncodedPostData _PostData = null; 
    StringBuffer writer = new StringBuffer(); 
    public void setPOSTData(URLEncodedPostData data) { 
    _PostData = data; 
    } 
    public ServiceRequestThread(String URL, MyScreen screen) { 
    super(); 
    _Dest = screen; 
    _URL = URL; 
    } 
     public void run() { 

     try 
     { 
    String boundary = "SATBA"; 
    String twoHyphens = "--"; 
    String data1 = "{\"IMPORTING\":{ \"IN_COUNTRY_CODE\":\"US\"}}"; 
    String CRLF = "\r\n"; 
    byte[] encoded = Base64OutputStream.encode 
    ("User:password".getBytes(), 0, "User:password".length(), false,false); 

    "Prepare the data for post" 

    writer.append("--" + boundary).append(CRLF); 
    writer.append("Content-Disposition: form-data; name=\"param\"").append(
    CRLF); 
    writer.append("Content-Type: text/json; charset=" + "UTF-8").append(CRLF); 
    writer.append("Content-Transfer-Encoding: 8bit").append(CRLF); 
    writer.append("Request-Id:Abcd123456").append(CRLF); 
    writer.append("Request-Type:rfc_json").append(CRLF); 
    writer.append("function:00163E0136C01EE0AE8B059433A71727") 
    .append(CRLF); 
     writer.append(CRLF); 
    writer.append(data1).append(CRLF); 
     writer.append("--" + boundary + "--").append(CRLF); 
    String string = new String(writer); 

    HttpConnection conn1 = (HttpConnection)Connector.open(_URL,Connector.READ_WRITE); 
    conn1.setRequestMethod(HttpConnection.POST); 
    conn1.setRequestProperty("Authorization", "Basic "+ new String(encoded)); 
    conn1.setRequestProperty("Content-Type","multipart/mixed; boundary=" + boundary); 

    OutputStreamWriter osw = new OutputStreamWriter(conn1.openOutputStream(), "UTF-8"); 
    osw.write(string); 
    osw.flush(); 
    osw.close(); 

    int responseCode = conn1.getResponseCode(); 
    if (responseCode == HttpConnection.HTTP_OK) { 
    InputStream data = conn1.openInputStream(); 
    StringBuffer raw = new StringBuffer(); 
    byte[] buf = new byte[4096]; 
    int nRead = data.read(buf); 
    while (nRead > 0) { 
    raw.append(new String(buf, 0, nRead)); 
    nRead = data.read(buf); 
    } 
    _Dest.updateDestination(raw.toString()); 
    } else { 
    _Dest.updateDestination("responseCode=" 
    + Integer.toString(responseCode)); 
    } 
    } 
    catch(IOException e) 
     { 
    e.printStackTrace(); 
    _Dest.updateDestination("Exception:"+e.toString()); 
    } 
    } 
      } 
+0

没有告诉我们实际的网址(对于*网址最多,HTTP响应预期正常),你不能指望我们提供帮助。至少告诉我们页面回复应该是什么样子的,它的提供者声称什么样的请求会引发你想要的JSON响应。 – bdares

+0

感谢您的快速回复:)我期待下面的JSON响应{\“EXPORTING \”:{\“OUT_CATEGORY_DISCRIPTION [] \”:\“SALES \”}}“; – talk2raghurao

+0

我相信网址存在问题.i曾经面临同样的问题因为url或可能是post请求不正确sent.have一看 – Swati

回答

2

原来的代码是完全没有问题,但问题是在哪里的application.handler.http.AuthenticationSupport设置为true正因为如此它不是在loggging的rim.public属性文件。

现在我将它设置为false并得到正确的回应。

+0

请不要张贴的答案,只是感谢某人为正确答案。使用评论功能,而不是评论正确的答案,并赞扬/标记为正确的答案:) –

+1

@bjorn - 这是一个答案,自我回答。 – Kev

+0

在编辑之前,它更像是一个谢谢我的帖子:/ –