servlet接收JSON帖子,但不显示

时间:2018-09-05 15:02:07

标签: javascript java post servlets

我正在JAVA上做

    protected void doPost(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("TEST TOMCAT SERVLET");
    StringBuilder data = new StringBuilder();
    String s;
    while ((s = request.getReader().readLine()) != null) {
        data.append(s);
    }
    data.toString(); //Datos que llegan

  }

}

当我初始化应用程序时。我在Servlet中收到: “ TOMCAT SERVLET测试” 但是我不是这个意思:

 return fetch(PUSH_ENDPOINT, {
  method: 'POST',
  headers: {
  Accept: 'application/json',
  'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  token: token,
  user: 'Brent'
  })
});

}

2 个答案:

答案 0 :(得分:0)

private UartDeviceCallback mUartCallback = new UartDeviceCallback() { @Override public boolean onUartDeviceDataAvailable(UartDevice uart) { // Read available data from the UART device Log.i(TAG, "onUartDeviceDataAvailable"); try { readUartBuffer(uart); } catch (IOException e) { Log.w(TAG, "Unable to access UART device", e); } // Continue listening for more interrupts return true; } private void readUartBuffer(UartDevice uart) throws IOException { // Maximum amount of data to read at one time Log.i(TAG, "readUartBuffer"); final int maxCount = 64; byte[] buffer = new byte[maxCount]; byte[] convertBuf = new byte[20]; boolean dataCompleteFlag = false; uart.read(buffer, maxCount); if (!dataCompleteFlag) { for (int i = 0; i < maxCount; i++) { if (buffer[i] == 36) { dataCompleteFlag = true; measureCount++; dataCount = 0; } else if(dataCount > maxCount) { dataCount = 0; } else if(buffer[i] != 0) { finalDataBuffer[dataCount] = buffer[i]; dataCount++; } } } if (dataCompleteFlag) { Log.i(TAG, Arrays.toString(finalDataBuffer)); int index = 0; int offSet; while (index < maxCount && finalDataBuffer[index] != 32) { convertBuf[index] = finalDataBuffer[index]; index++; } index++; offSet = index; tankLevelString = new String(convertBuf); Float tankFloat = parseFloat(tankLevelString); tankLevelString += "cm"; while (index < maxCount && finalDataBuffer[index] != 32) { convertBuf[index - offSet] = finalDataBuffer[index]; index++; } index++; offSet = index; airTempString = new String(convertBuf); airTempString += "\u00b0C"; while (index < maxCount && finalDataBuffer[index] != 32) { convertBuf[index - offSet] = finalDataBuffer[index]; index++; } index++; offSet = index; humidityString = new String(convertBuf); humidityString += "%"; while (index < maxCount && finalDataBuffer[index] != 32) { convertBuf[index - offSet] = finalDataBuffer[index]; index++; } index++; offSet = index; ecString = new String(convertBuf); while (index < maxCount && finalDataBuffer[index] != 32) { convertBuf[index - offSet] = finalDataBuffer[index]; index++; } index++; offSet = index; phString = new String(convertBuf); while (index < maxCount && finalDataBuffer[index] != 0) { convertBuf[index - offSet] = finalDataBuffer[index]; index++; } waterTempString = new String(convertBuf); TextView airTemp = findViewById(R.id.airTemp); airTemp.setText(airTempString); TextView humidity = findViewById(R.id.humidity); humidity.setText(humidityString); TextView tankLevel1 = findViewById(R.id.tankLevel1); tankLevel1.setText(tankLevelString); TextView ec = findViewById(R.id.ec); ec.setText(ecString); TextView ph = findViewById(R.id.ph); ph.setText(phString); TextView wt = findViewById(R.id.waterTemp); wt.setText(waterTempString); } } @Override public void onUartDeviceError(UartDevice uart, int error) { Log.w(TAG, uart + ": Error event " + error); } }; 并没有满足您的要求。

执行toString()

答案 1 :(得分:0)

您需要将响应写到客户端。这是一个示例,您可以怎么做:

   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println("<h1>Hello World</h1>");
   }