HttpURLConnection 应用程序因 ESP8266 而崩溃
HttpURLConnection application crashes with ESP8266
我有一个带有以下 Lua 脚本的 ESP8266 简单 http 服务器
print("My First Lua program")
--print(adc.readvdd33())
print("Setting Wifi")
wifi.setmode(wifi.STATIONAP) --[[ STATION + AP --]]
wifi.setphymode(wifi.PHYMODE_N) --[[ IEEE 802.n --]]
print(wifi.getmode())
print(wifi.getphymode())
wifi.sta.config("srs", "cometomyn/w0")
tmr.delay(5000000)
print("Delay out")
--print(wifi.sta.getip())
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> ESP8266<BR>Server is working!</h1>")
conn:close()
end)
end)
当我使用 chrome 通过笔记本电脑连接到服务器时,我收到 "Server is working!" 响应。
但是当我通过我制作的 android 应用程序连接时,它崩溃了 :( 。以下是我的应用程序代码
public class HttpManager {
public static String downloadUrl(String uri) throws IOException {
HttpURLConnection con = null;
InputStream is=null;
try {
URL url = new URL(uri);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
//add request header
//con.setRequestProperty("User-Agent", "Mozilla/5.0");
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
is = con.getInputStream();
}catch (IOException e) {
e.printStackTrace();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
我在异步任务中调用这个 HttpManager。使用此应用程序,我能够从 google 等网站获得响应! .
我不确定是哪个代码有问题!!!
谁能帮我解决这个问题?
也追加崩溃日志
07-18 11:51:04.122 3710-3710/srs.thebewboston I/First success﹕ http
07-18 11:51:04.312 3710-3985/srs.thebewboston W/System.err﹕ java.io.EOFException
07-18 11:51:04.322 3710-3985/srs.thebewboston W/System.err﹕ at libcore.io.Streams.readAsciiLine(Streams.java:203)
07-18 11:51:04.322 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
07-18 11:51:04.332 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
07-18 11:51:04.332 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:38)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
07-18 11:51:04.382 3710-3985/srs.thebewboston W/System.err﹕ at android.os.AsyncTask.call(AsyncTask.java:264)
07-18 11:51:04.382 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-18 11:51:04.412 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-18 11:51:04.452 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-18 11:51:04.472 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-18 11:51:04.592 3710-3985/srs.thebewboston W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
07-18 11:51:04.612 3710-3985/srs.thebewboston W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
07-18 11:51:04.652 3710-3985/srs.thebewboston E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:43)
at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
at android.os.AsyncTask.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
同时添加我的服务器响应
GET Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
GET Dalvik/1.6.0 (Linux; U; Android 4.4.4; MI 3W MIUI/V6.5.3.0.KXDMICD)
GET Mozilla/5.0 (Linux; Android 4.4.4; MI 3W Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.93 Mobile Safari/537.36
我在笔记本电脑上尝试使用 chrome 时的第一个响应(有效)
当我尝试从移动设备上使用我的应用程序时的第二个响应(无效!!)
当我尝试从同一部手机使用 chrome 时的第三次响应(有效)
我的Assynctask调用部分
private class MyATask extends AsyncTask<String, String, String>{
TextView testout = (TextView) findViewById(R.id.testout);
@Override
protected void onPreExecute(){
testout.append("Starting Task" + '\n');
}
@Override
protected String doInBackground(String... params) {
String httout = null;
try {
httout = HttpManager.downloadUrl(params[0]);
} catch (IOException e) {
e.printStackTrace();}
return httout;}
@Override
protected void onProgressUpdate(String... values) {
//testout.append(values[0]+'\n');}
@Override
protected void onPostExecute(String result) {
testout.append(result + '\n');}
}
Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.1.3"); //这个没用
Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://www.google.com"); //这个有效
谢谢
这段代码基本上存在三个问题:
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> ESP8266<BR>Server is working!</h1>")
conn:close()
end)
conn:send()
是异步的,见API docs。这意味着您不能在调用 conn:send()
后立即关闭连接,因为在您关闭连接时数据可能尚未发送。
- 您在两个回调函数中重复使用了
conn
变量。因此,将 function(conn,payload)
更改为 function(whatever,payload)
,然后相应地执行 whatever:send
。
- 您发回的不是有效的 HTTP 响应,而只是一些与协议无关的 HTML 片段。
查看 https://github.com/nodemcu/nodemcu-firmware/blob/dev/README.md#programming-model 以获得完整的工作示例。
我有一个带有以下 Lua 脚本的 ESP8266 简单 http 服务器
print("My First Lua program")
--print(adc.readvdd33())
print("Setting Wifi")
wifi.setmode(wifi.STATIONAP) --[[ STATION + AP --]]
wifi.setphymode(wifi.PHYMODE_N) --[[ IEEE 802.n --]]
print(wifi.getmode())
print(wifi.getphymode())
wifi.sta.config("srs", "cometomyn/w0")
tmr.delay(5000000)
print("Delay out")
--print(wifi.sta.getip())
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> ESP8266<BR>Server is working!</h1>")
conn:close()
end)
end)
当我使用 chrome 通过笔记本电脑连接到服务器时,我收到 "Server is working!" 响应。
但是当我通过我制作的 android 应用程序连接时,它崩溃了 :( 。以下是我的应用程序代码
public class HttpManager {
public static String downloadUrl(String uri) throws IOException {
HttpURLConnection con = null;
InputStream is=null;
try {
URL url = new URL(uri);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
//add request header
//con.setRequestProperty("User-Agent", "Mozilla/5.0");
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
is = con.getInputStream();
}catch (IOException e) {
e.printStackTrace();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
我在异步任务中调用这个 HttpManager。使用此应用程序,我能够从 google 等网站获得响应! .
我不确定是哪个代码有问题!!! 谁能帮我解决这个问题?
也追加崩溃日志
07-18 11:51:04.122 3710-3710/srs.thebewboston I/First success﹕ http
07-18 11:51:04.312 3710-3985/srs.thebewboston W/System.err﹕ java.io.EOFException
07-18 11:51:04.322 3710-3985/srs.thebewboston W/System.err﹕ at libcore.io.Streams.readAsciiLine(Streams.java:203)
07-18 11:51:04.322 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
07-18 11:51:04.332 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
07-18 11:51:04.332 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:38)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
07-18 11:51:04.342 3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
07-18 11:51:04.382 3710-3985/srs.thebewboston W/System.err﹕ at android.os.AsyncTask.call(AsyncTask.java:264)
07-18 11:51:04.382 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-18 11:51:04.412 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-18 11:51:04.452 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-18 11:51:04.472 3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-18 11:51:04.592 3710-3985/srs.thebewboston W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
07-18 11:51:04.612 3710-3985/srs.thebewboston W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
07-18 11:51:04.652 3710-3985/srs.thebewboston E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:43)
at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
at android.os.AsyncTask.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
同时添加我的服务器响应
GET Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
GET Dalvik/1.6.0 (Linux; U; Android 4.4.4; MI 3W MIUI/V6.5.3.0.KXDMICD)
GET Mozilla/5.0 (Linux; Android 4.4.4; MI 3W Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.93 Mobile Safari/537.36
我在笔记本电脑上尝试使用 chrome 时的第一个响应(有效)
当我尝试从移动设备上使用我的应用程序时的第二个响应(无效!!)
当我尝试从同一部手机使用 chrome 时的第三次响应(有效)
我的Assynctask调用部分
private class MyATask extends AsyncTask<String, String, String>{
TextView testout = (TextView) findViewById(R.id.testout);
@Override
protected void onPreExecute(){
testout.append("Starting Task" + '\n');
}
@Override
protected String doInBackground(String... params) {
String httout = null;
try {
httout = HttpManager.downloadUrl(params[0]);
} catch (IOException e) {
e.printStackTrace();}
return httout;}
@Override
protected void onProgressUpdate(String... values) {
//testout.append(values[0]+'\n');}
@Override
protected void onPostExecute(String result) {
testout.append(result + '\n');}
}
Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.1.3"); //这个没用 Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://www.google.com"); //这个有效
谢谢
这段代码基本上存在三个问题:
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> ESP8266<BR>Server is working!</h1>")
conn:close()
end)
conn:send()
是异步的,见API docs。这意味着您不能在调用conn:send()
后立即关闭连接,因为在您关闭连接时数据可能尚未发送。- 您在两个回调函数中重复使用了
conn
变量。因此,将function(conn,payload)
更改为function(whatever,payload)
,然后相应地执行whatever:send
。 - 您发回的不是有效的 HTTP 响应,而只是一些与协议无关的 HTML 片段。
查看 https://github.com/nodemcu/nodemcu-firmware/blob/dev/README.md#programming-model 以获得完整的工作示例。