我在Android
上编写了一个应用程序,该应用程序实现了将简单的请求(使用Volley
)发送到服务器。服务器安装在用Lua
编写的NodeMCU(ESP8266)微控制器上。问题是,在发送请求之后,应用程序并不总是能够打印响应。如果地址是例如“ http://www.google.com”可以正确发送请求并接收和显示响应,但是如果它是下面代码中的地址-可以正确发送请求(服务器做出反应)但不(?)接收响应(不显示请求,显示:“那没用!”)。您有什么想法,我该如何解决并能够打印响应?
Android(负责发送请求的部分):
buttonSynchro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Instantiate the RequestQueue.
String url = "http://192.168.1.12/";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
testTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
testTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(SettingsActivity.this);
queue.add(stringRequest);
}
});
Lua NodeMCU:
station_cfg={}
station_cfg.ssid="Dom"
station_cfg.pwd="lalala"
wifi.sta.config(station_cfg)
function receive(conn, request)
print(request)
print()
local buf = "";
buf = buf.."<!doctype html><html>";
buf = buf.."<h1> ESP8266 Web Server</h1>";
buf = buf.."</html>";
conn:send(buf);
conn:on("sent", function(sck) sck:close() end);
collectgarbage();
end
function connection(conn)
conn:on("receive", receive)
end
srv=net.createServer(net.TCP, 30)
srv:listen(80, connection)
最佳答案
nPn的代码可在某些用户代理(macOS上为Chrome / Firfox / curl / wget)上运行,而在其他用户代理(macOS和iOS上为Safari,iOS上为Firefox Klar)下则无效。这可能是由于缺少HTTP标头。
我建议您坚持使用https://nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketsend文档中的示例。
srv = net.createServer(net.TCP)
function receiver(sck, data)
print(data)
print()
-- if you're sending back HTML over HTTP you'll want something like this instead
local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}
response[#response + 1] = "<!doctype html><html>"
response[#response + 1] = "<h1> ESP8266 Web Server</h1>"
response[#response + 1] = "</html>"
-- sends and removes the first element from the 'response' table
local function send(localSocket)
if #response > 0 then
localSocket:send(table.remove(response, 1))
else
localSocket:close()
response = nil
end
end
-- triggers the send() function again once the first chunk of data was sent
sck:on("sent", send)
send(sck)
end
srv:listen(80, function(conn)
conn:on("receive", receiver)
end)
另外,您的代码(以及相应的nPn)假设WiFi在不应该使用的地方可用。
wifi.sta.config(station_cfg)
(with auto-connect=true) and wifi.stat.connect
是异步的,因此是非阻塞的-与许多其他NodeMCU API一样。因此,您应该将上述代码放入函数中,并且仅在设备连接到AP并获得IP后才调用它。您可以通过例如在WiFi事件监视器中注册STA_GOT_IP
事件的回调。您会找到一个非常详细的引导序列示例,该引导序列在https://nodemcu.readthedocs.io/en/latest/en/upload/#initlua处监听所有WiFi事件。对于初学者来说,您可能需要调整它,仅收听get-IP。关于java - Android和NodeMCU,无法接收来自服务器的响应吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49203229/