Java HttpURLConnection应用程序与ESP8266一起崩溃
我有一个ESP8266简单http服务器,带有以下Lua脚本Java HttpURLConnection应用程序与ESP8266一起崩溃,java,android,lua,esp8266,nodemcu,Java,Android,Lua,Esp8266,Nodemcu,我有一个ESP8266简单http服务器,带有以下Lua脚本 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("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)
}
我在一个同步任务中调用这个HttpManager。有了这个应用程序,我可以从像谷歌这样的网站上得到回复
我不确定哪个代码有问题!!!
谁能帮我解决这个问题
还附加了崩溃日志
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$2.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$3.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$2.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)
当我尝试从笔记本电脑使用chrome时的第一个响应(已工作)
当我尝试从手机上使用我的应用程序时的第二个响应(未工作!!)
当我在同一部手机上试用chrome时的第三个响应(已工作)
我的助理任务呼叫区
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');}
}
私有类MyATask扩展异步任务{
TextView testout=(TextView)findViewById(R.id.testout);
@凌驾
受保护的void onPreExecute(){
append(“启动任务”+'\n');
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串httout=null;
试一试{
httout=HttpManager.downloadUrl(参数[0]);
}捕获(IOE异常){
e、 printStackTrace();}
返回httout;}
@凌驾
受保护的void onProgressUpdate(字符串…值){
//追加(值[0]+'\n');}
@凌驾
受保护的void onPostExecute(字符串结果){
追加(结果+'\n');}
}
Atask.executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,“”)//这个不行
Atask.executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,“”)//这个成功了
谢谢此代码基本上有三个问题:
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)
srv:监听(80,功能(conn)
连接:打开(“接收”,功能(连接,有效负载)
打印(有效载荷)
连接:发送(“ESP8266
服务器正在工作!”)
康涅狄格州:关闭
(完)
conn:send()
是异步的,请参阅。这意味着您无法在调用conn:send()
后立即关闭连接,因为在关闭连接时数据可能尚未发送conn
变量。因此,将function(conn,payload)
更改为function(whatever,payload)
,然后相应地执行whatever:send
查看完整的工作示例。
downloadUrl(HttpManager.java:38)
。哪一行代码?catch(IOE异常)
。如果有问题,不应继续执行以下代码,而应返回。我正在使用获取所需的URL调用HttpManager,目前我提供ESP8266模块的IP地址,如下所示:Atask.executeOnExecutor(AsyncTask.THREAD\u POOL\u EXECUTOR,“);java.lang.NullPointerException
。您有这个异常,因为您在捕获之后继续执行该操作。@sreejith您可以记录从服务器返回的内容以及对其余代码的注释。它是否击中了服务器。我怀疑这一点
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');}
}
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)