java - HttpURLConnection application crashes with ESP8266 -


i have esp8266 simple http server following lua script

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 working!</h1>")         conn:close()         end)  end) 

when connect server through laptop chrome, getting "server working!" response.

but when connect through android app made, crashing :( . following app code

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");         = 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; } 

}

i calling httpmanager in assync task. app able response sites google ! .

i not sure code having issue !!! can me solve issue ?

appending crash log too

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﹕ @ libcore.io.streams.readasciiline(streams.java:203) 07-18 11:51:04.322    3710-3985/srs.thebewboston w/system.err﹕ @ libcore.net.http.httpengine.readresponseheaders(httpengine.java:544) 07-18 11:51:04.332    3710-3985/srs.thebewboston w/system.err﹕ @ libcore.net.http.httpengine.readresponse(httpengine.java:784) 07-18 11:51:04.332    3710-3985/srs.thebewboston w/system.err﹕ @ libcore.net.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:274) 07-18 11:51:04.342    3710-3985/srs.thebewboston w/system.err﹕ @ libcore.net.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:168) 07-18 11:51:04.342    3710-3985/srs.thebewboston w/system.err﹕ @ srs.thebewboston.httpmanager.downloadurl(httpmanager.java:38) 07-18 11:51:04.342    3710-3985/srs.thebewboston w/system.err﹕ @ srs.thebewboston.mainactivity$myatask.doinbackground(mainactivity.java:278) 07-18 11:51:04.342    3710-3985/srs.thebewboston w/system.err﹕ @ srs.thebewboston.mainactivity$myatask.doinbackground(mainactivity.java:264) 07-18 11:51:04.382    3710-3985/srs.thebewboston w/system.err﹕ @ android.os.asynctask$2.call(asynctask.java:264) 07-18 11:51:04.382    3710-3985/srs.thebewboston w/system.err﹕ @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 07-18 11:51:04.412    3710-3985/srs.thebewboston w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:137) 07-18 11:51:04.452    3710-3985/srs.thebewboston w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 07-18 11:51:04.472    3710-3985/srs.thebewboston w/system.err﹕ @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 07-18 11:51:04.592    3710-3985/srs.thebewboston w/system.err﹕ @ java.lang.thread.run(thread.java:856) 07-18 11:51:04.612    3710-3985/srs.thebewboston w/dalvikvm﹕ threadid=12: thread exiting uncaught exception (group=0x409c01f8) 07-18 11:51:04.652    3710-3985/srs.thebewboston e/androidruntime﹕ fatal exception: asynctask #1     java.lang.runtimeexception: error occured while executing doinbackground()             @ android.os.asynctask$3.done(asynctask.java:278)             @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273)             @ java.util.concurrent.futuretask.setexception(futuretask.java:124)             @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307)             @ java.util.concurrent.futuretask.run(futuretask.java:137)             @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076)             @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569)             @ java.lang.thread.run(thread.java:856)      caused by: java.lang.nullpointerexception             @ java.io.reader.<init>(reader.java:64)             @ java.io.inputstreamreader.<init>(inputstreamreader.java:122)             @ java.io.inputstreamreader.<init>(inputstreamreader.java:59)             @ srs.thebewboston.httpmanager.downloadurl(httpmanager.java:43)             @ srs.thebewboston.mainactivity$myatask.doinbackground(mainactivity.java:278)             @ srs.thebewboston.mainactivity$myatask.doinbackground(mainactivity.java:264)             @ android.os.asynctask$2.call(asynctask.java:264)             @ 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) 

adding server response also

get  mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.132 safari/537.36  dalvik/1.6.0 (linux; u; android 4.4.4; mi 3w miui/v6.5.3.0.kxdmicd)  mozilla/5.0 (linux; android 4.4.4; mi 3w build/ktu84p) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.93 mobile safari/537.36 

1st response when tried chrome laptop (worked)

2nd response when tried app mobile (not worked!!)

3rd response when tried chrome same mobile (worked)

my assynctask calling section

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"); //this 1 didn't work atask.executeonexecutor(asynctask.thread_pool_executor,"http://www.google.com"); //this 1 worked

thanks

there 3 issues code:

srv:listen(80,function(conn)      conn:on("receive",function(conn,payload)          print(payload)          conn:send("<h1> esp8266<br>server working!</h1>")         conn:close()         end) 
  1. conn:send() asynchronous, see api docs. means can't close connection after calling conn:send() because data might not have been sent time close connection.
  2. you reuse conn variable in 2 callback functions. so, change function(conn,payload) function(whatever,payload) , whatever:send accordingly.
  3. what you're sending not valid http response protocol agnostic html snippet.

look @ https://github.com/nodemcu/nodemcu-firmware/blob/dev/readme.md#programming-model complete , working example.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -