2016-07-19 2 views
0

안드로이드 노드 :전송 JSON 배열은 서버에게

try { 
       JSONObject obj = new JSONObject(); 
       obj.put("id", editid.getText().toString()); 
       obj.put("pw", editpassword.getText().toString()); 
       final String jsonstring = obj.toString(); 
       //final String jsonstring = "{\"id=\":\"abce\",\"pw\":\"13447\"}"; 
       Log.i("tag",jsonstring); 
       Thread background = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          Log.i("tag", "Thread"); 
          URL url = new URL("http://49.172.86.247:80/login/controller_on"); 
          HttpURLConnection hey = (HttpURLConnection) url.openConnection(); 
          hey.setRequestMethod("POST"); 
          hey.setDoInput(true); 
          hey.setDoOutput(true); 
          hey.setRequestProperty("Content-Type", "application/json"); 
          hey.setConnectTimeout(10000); 
          hey.setUseCaches(false); 

          DataOutputStream wr = new DataOutputStream(hey.getOutputStream()); 

          InputStream inputStream = hey.getInputStream(); 
          wr.writeBytes(jsonstring.toString()); 
          wr.flush(); 
          wr.close(); 

         } catch (Exception e) { 
          Log.e("tag","error"); 
         } 

서버 :

app.post('/login/:termType', function(req, res){ 
    console.log(req.body); 
}); 

난 단지 노드 명령 프롬프트에서 {} 볼 수 있습니다. 그러나 양식 태그를 통해 html에서 서버로 데이터를 보내는 것은 작동합니다. 뭐가 문제 야?

+0

express에서 json을 구문 분석하기 위해 body-parser middleware를 사용하십시오. –

+0

나는 이미 포함하여 {id : 'test', password : 'test'}와 같은 html 데이터를받을 수 있습니다. 하지만 Android에서 데이터를 수신 할 수 없습니다. –

답변

0

Android에서 검색하려면 노드 서버에서 데이터를 반환해야합니다.

return res.status(201).json({ 
    success: true, 
    data: req.body, 
    message:'The response' 
})