2011-09-01 34 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

在編輯之前,它更像是一個謝謝我的帖子:/ –