【发布时间】:2018-08-18 14:01:16
【问题描述】:
我在Android 上编写了一个应用程序,它实现了向服务器发送简单的请求(使用Volley)。服务器安装在 NodeMCU (ESP8266) 微控制器上,使用 Lua 编写。问题是,在发送请求后,应用程序并不总是能够打印响应。如果地址是例如“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);
}
});
NodeMCU、Lua:
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)
【问题讨论】:
-
请检查 192.168.1.12 在您的浏览器中是否正常工作。 :-)
-
是的,当然可以。我以为我已经说过了:)
-
顺便说一句,您确实没有说它在浏览器中工作,您说服务器“做出反应”,我认为这意味着您看到了打印在 esp 上的请求。
-
@nPn 是的,我没有直接说。所以说清楚:发送请求后,我看到请求打印在 esp 上,通过浏览器发送请求后,我看到上面显示的 HTML 代码页面。
标签: java android lua esp8266 nodemcu