Javascript 为什么js服务器不接收HTTP post请求?
我正在编写一个android应用程序,它通过http向运行在pc上的nodeJs服务器发送一个.wav文件。nodeJs服务器在收到POST请求时只需在屏幕上打印一条消息。它还打印收到的文件的内容。在Android端,单击按钮时会向服务器发出包含文件的POST请求。问题是,当点击按钮时,服务器从未收到任何东西。我已经尝试过使用邮递员软件发出POST请求,效果很好 android代码:Javascript 为什么js服务器不接收HTTP post请求?,javascript,android,node.js,http,Javascript,Android,Node.js,Http,我正在编写一个android应用程序,它通过http向运行在pc上的nodeJs服务器发送一个.wav文件。nodeJs服务器在收到POST请求时只需在屏幕上打印一条消息。它还打印收到的文件的内容。在Android端,单击按钮时会向服务器发出包含文件的POST请求。问题是,当点击按钮时,服务器从未收到任何东西。我已经尝试过使用邮递员软件发出POST请求,效果很好 android代码: public class MainActivity extends Activity { @Overr
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("MY TAG: ", "SENDING....");
send();
Log.d("MY TAG: ", "SENT.");
}
});
}
void send(){
String url = "http://"my ip goes here":4000/extract_indicators/audio/3";
File exStore = Environment.getExternalStorageDirectory();
File file = new File(exStore.getAbsolutePath() + "/AudioRecord/recording0.wav");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
Log.d("HTTP ", "RESPONSE: " + response);
} catch(Exception e) {
// show error
}
}
}
NodeJs代码:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
const fs = require('fs');
const wav = require('wav');
let upload = multer();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.raw({ type: 'audio/wav', limit: '50mb' }));
app.post('/extract_indicators/audio/:user_id', upload.any(), function(req, res) {
console.log("RECIEVED AUDIO TO EXTRACT INDICATORS: ", req.body);
let formData = req.body;
let files = req.files;
//fs.writeFileSync('audiotest.wav', req.files[1].buffer);
console.log('form data', formData, 'file' , files);
res.sendStatus(200);
});
app.listen(4000);
按下按钮时的Logcat:
D/ViewRootImpl@ad416c[MainActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@ad416c[MainActivity]: ViewPostImeInputStage processPointer 1
D/MY TAG:: SENDING....
D/MY TAG:: SENT.
编辑:
这是原始问题的第一个答案之后的编辑代码。然而,我仍然有同样的问题
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnSend);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("MY TAG: ", "SENDING....");
new wavSender().execute();
Log.d("MY TAG: ", "SENT.");
}
});
}
private class wavSender extends AsyncTask<String, Void, String> {
public wavSender() {
}
@Override
protected String doInBackground(String... strings) {
String url = "http://my ip goes here:4000/extract_indicators/audio/3";
File exStore = Environment.getExternalStorageDirectory();
File file = new File(exStore.getAbsolutePath() + "/AudioRecord/recording0.wav");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
Log.d("HTTP ", "RESPONSE: " + response);
} catch(Exception e) {
// show error
}
return null;
}
@Override
protected void onPostExecute(String temp) {
Log.d("MY TAG", "POST EXECUTE HAS RAN");
}
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮btn=findViewById(R.id.btnSend);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Log.d(“我的标签:,“发送…”);
新建wavSender().execute();
Log.d(“我的标签:,“已发送”);
}
});
}
私有类wavSender扩展异步任务{
公共信息发送者(){
}
@凌驾
受保护的字符串背景(字符串…字符串){
字符串url=”http://my ip在这里:4000/提取指示灯/音频/3”;
文件exStore=Environment.getExternalStorageDirectory();
File File=新文件(exStore.getAbsolutePath()+“/AudioRecord/recording0.wav”);
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
InputStreamEntity reqEntity=新的InputStreamEntity(
新文件输入流(文件),-1);
requentity.setContentType(“二进制/八位字节流”);
reqEntity.setChunked(true);//如果需要,发送多个部分
httppost.setEntity(reqEntity);
HttpResponse response=httpclient.execute(httppost);
//用回应做点什么。。。
Log.d(“HTTP”,“RESPONSE:”+RESPONSE);
}捕获(例外e){
//显示错误
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串温度){
Log.d(“我的标签”,“执行后已运行”);
}
}
}
您可能正在主线程内执行网络函数,它可能会抛出错误,但您使用try catch
抑制了该错误。您可以使用AsyncTask
执行网络调用,请参阅此链接了解如何使用它:我现在通过一个异步任务执行此操作,但是我在logcat或调试器中得到了相同的行为,没有抛出任何错误。请记录异常。它包含有用的信息,而不是什么都不做。非常感谢,原来http客户端抛出了一个拒绝访问的异常。但是,AsyncTask至少可以正确执行网络调用。