服务器运行本地代码,同样一段代码,在本地运行好使,在云服务器运行不好使...

这是一个发送post请求的工具类,在本地可以正常获取到返回的数据,上传到服务器后,无法获取到数据,而且日志记录显示未发生异常

public static JSONObject doPost(String url,JSONObject json){

DefaultHttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(url);

JSONObject response = null;

try {

StringEntity s = new StringEntity(json.toString());

s.setContentEncoding("UTF-8");

s.setContentType("application/json");//发送json数据需要设置contentType

post.setEntity(s);

HttpResponse res = client.execute(post);

if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){

String result = EntityUtils.toString(res.getEntity());// 返回json格式:

logger.info("result:"+result);

response = JSON.parseObject(result);

}

} catch (Exception e) {

logger.error("error:"+e.getMessage());

e.printStackTrace();

}

logger.info("请求结束");

return response;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,请稍等,我正在为您生成代码。 ```cpp #include <WiFi.h> #include <PubSubClient.h> #include "driver/i2s.h" #define WIFI_SSID "your-WiFi-SSID" #define WIFI_PASS "your-WiFi-password" #define MQTT_SERVER "your-MQTT-server-IP" #define MQTT_PORT 1883 #define MQTT_USER "your-MQTT-username" #define MQTT_PASS "your-MQTT-password" // Number of samples to read at once #define SAMPLES_PER_READ 128 // I2S Constants static constexpr int SAMPLE_RATE = 16000; // 16 kHz static constexpr int BITS_PER_SAMPLE = 16; // Ring buffer constants static constexpr int RING_BUFFER_SIZE = 8192; static uint8_t ringBuffer[RING_BUFFER_SIZE] = {0}; static volatile uint32_t ringBufferIndex = 0; // Wifi client WiFiClient wifiClient; // MQTT client PubSubClient mqttClient(wifiClient); // Interrupt that reads samples from the I2S microphone void IRAM_ATTR i2s_isr() { static constexpr int BYTES_PER_SAMPLE = BITS_PER_SAMPLE / 8; static uint8_t buffer[SAMPLES_PER_READ * BYTES_PER_SAMPLE] = {0}; size_t bytesRead = i2s_read(I2S_NUM_0, buffer, SAMPLES_PER_READ * BYTES_PER_SAMPLE, portMAX_DELAY); if (bytesRead > 0) { for (size_t i = 0; i < bytesRead; i += BYTES_PER_SAMPLE) { // Store the data in the ring buffer if (ringBufferIndex >= RING_BUFFER_SIZE) { // Buffer overflow ringBufferIndex = 0; } ringBuffer[ringBufferIndex] = buffer[i]; ringBufferIndex++; } } } void setup() { // Connect to WiFi WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { delay(1000); } // Set up MQTT client mqttClient.setServer(MQTT_SERVER, MQTT_PORT); mqttClient.setCredentials(MQTT_USER, MQTT_PASS); while (!mqttClient.connected()) { mqttClient.connect("ESP32"); delay(1000); } // Set up IIS i2s_config_t i2sConfig = { .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = SAMPLE_RATE, .bits_per_sample = i2s_bits_per_sample_t(BITS_PER_SAMPLE), .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = I2S_COMM_FORMAT_I2S, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 2, .dma_buf_len = SAMPLES_PER_READ * BITS_PER_SAMPLE / 8, .use_apll = false}; i2s_pin_config_t i2sPins = { .bck_io_num = 26, .ws_io_num = 25, .data_out_num = I2S_PIN_NO_CHANGE, .data_in_num = 33}; i2s_driver_install(I2S_NUM_0, &i2sConfig, 0, nullptr); i2s_set_pin(I2S_NUM_0, &i2sPins); i2s_zero_dma_buffer(I2S_NUM_0); // Set up interrupt esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_IRAM, &i2s_isr, nullptr, nullptr); } void loop() { // Wait until we have enough data while (ringBufferIndex < RING_BUFFER_SIZE / 2) { delay(10); } // Prepare the data for MQTT uint8_t data[RING_BUFFER_SIZE] = {0}; uint32_t dataIndex = 0; for (uint32_t i = ringBufferIndex; i < RING_BUFFER_SIZE; i++) { data[dataIndex] = ringBuffer[i]; dataIndex++; } for (uint32_t i = 0; i < ringBufferIndex; i++) { data[dataIndex] = ringBuffer[i]; dataIndex++; } // Publish the data to MQTT mqttClient.publish("esp32/iis-recording", data, RING_BUFFER_SIZE); } ``` 这是用于 ESP32 的 IIS 录音并上传至 MQTT 服务器代码。请将其中的 WIFI_SSID、WIFI_PASS、MQTT_SERVER、MQTT_PORT、MQTT_USER 和 MQTT_PASS 改为您自己的 Wi-Fi 和 MQTT 信息,并将这段代码上传到 ESP32 即可开始录音并上传数据到 MQTT 服务器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值