2016-07-05 81 views
-2

這是我的第一個項目。當我使用,我無法發送數據到服務器。android使用HttpPost和StringEntity

這裏是我的代碼:

JSONObject json = new JSONObject(); 
     try 
     {   json.put("_username", mUsername); 
        json.put("_password", mPassword); 
        String s=json.toString(); 
        System.out.println("string: " + s); 
        StringEntity se = new StringEntity(s,HTTP.UTF_8); 
        System.out.println("0.string: " + se); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded")); 
        System.out.println("1.string: " + se); 
        httpRequest.setEntity(se); 

catch (Exception e) 這裏是logcat的

07-05 13:09:57.731 16809-16809/com.ad_imagine.jsonconnection I/System.out: string: {"_username":"[email protected]","_password":"aze"} 
07-05 13:09:57.733 16809-16809/com.ad_imagine.jsonconnection I/System.out: 0.string: [email protected] 
07-05 13:09:57.733 16809-16809/com.ad_imagine.jsonconnection I/System.out: 1.string: [email protected] 
07-05 13:09:57.768 16809-16809/com.ad_imagine.jsonconnection I/System.out: 6.Exception: null 

什麼是[email protected]?爲什麼0.string不正確?

+0

你爲什麼要發送JSON數據,但告訴服務器其形式數據? – larsgrefer

+0

你能澄清你想要你的POST請求的樣子嗎? – larsgrefer

回答

0

什麼是[email protected]

[email protected]se.toString()的結果。 StringEntity似乎並沒有覆蓋toString()因此使用的Object.toString()實施剛剛返回<FullyQualifiedClassName>@<hashCode>

爲什麼0.string是不正確的?

你不能說它不正確。你必須閱讀se.getContent()

+0

你不必添加'se.toString()',編譯器爲你添加了這個。 'System.out.println(「0.string:」+ se);''和'System.out.println(「0.string:」+ se.toString());''是相同的。 您必須調用se.getContent()並從中讀取它。 – larsgrefer

+0

但如何更改代碼?我希望''是'_username = lorem @ ipsum.fr&_password = aze',然後我可以將它發送到服務器 –

+0

謝謝,你很有耐心 –