2016-01-21 56 views
0

我嘗試使用發送JSON從安卓到的NodeJS服務器

HttpURLConnection con = (HttpURLConnection) (new URL(IP + "/getrestaurant")).openConnection(); 
con.setRequestMethod("POST"); 
con.setRequestProperty("Accept", "application/json"); 
con.setDoInput(true); 
con.setDoOutput(true); 
con.connect(); 
con.getOutputStream().write("{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}".getBytes("UTF-8")); 
con.getOutputStream().flush(); 
con.getOutputStream().close(); 

從Android設備發送請求到的NodeJS服務器當我打印從節點JS接收到的請求,我得到:

{   
    "{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}": "" 
} 

而不是{'restaurant_id':'569a16e28dcdc5c8add2a8e0'}

我該如何得到它的工作?謝謝!

+0

'{「restaurant_id」:「569a16e28dcdc5c8add2a8e0」}'是不是有效的JSON ... – Selvin

回答

0

的評論說,(我有信譽太低,評論),{「restaurant_id」:「569a16e28dcdc5c8add2a8e0」}是無效的情況下

我也建議你使用Koush離子庫JSON請求。 https://github.com/koush/ion

這是非常方便和易於使用,試試看。在一個線上的所有類型的請求都做了,

0

的問題是,我需要con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");