2016-05-26 115 views
0

我已经制作了基于NodeMCU的小型服务器。所有的作品都很好,当我从浏览器连接时,但问题开始,当我尝试从Android应用程序uisng OkHttp或Volley连接时,我收到了异常。 java.io.IOException:使用OkHttp的Connection上流的意外结束, 使用Volley的EOFException。当从Android应用发送GET请求时,NodeMCU服务器响应不良

问题是非常相似的这 EOFException after server responds,但答案没有找到。

ESP服务器代码

srv:listen(80, function(conn) 

    conn:on("receive", function(conn,payload) 
    print(payload) 
    conn:send("<h1> Hello, NodeMCU.</h1>") 
    end) 
    conn:on("sent", function(conn) conn:close() end) 
end) 

的Android代码

final RequestQueue queue = Volley.newRequestQueue(this); 
    final String url = "http://10.42.0.17:80"; 

final StringRequest request = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 

       @Override 
       public void onResponse(String response) { 
        mTemperatureTextView.setText(response.substring(0, 20)); 
        System.out.println(response); 
       } 
      }, 

      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        System.out.println("Error + " + error.toString()); 
        mTemperatureTextView.setText("That didn't work!"); 
       } 
      } 
    ); 

mUpdateButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      queue.add(request); 
     } 
    }); 

回答

2

您发回的内容不是HTTP。它不过是一个与协议无关的HTML片段。此外,还有内存泄漏。

试试这个:

srv:listen(80, function(conn) 

    conn:on("receive", function(sck,payload) 
    print(payload) 
    sck:send("HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n<h1> Hello, NodeMCU.</h1>") 
    end) 
    conn:on("sent", function(sck) sck:close() end) 
end) 
  • 你需要发回的HTTP标头,HTTP/1.0 200 OK和换行符是强制性
  • 每个功能需要使用它自己传递的Socket实例的副本,请我如何在这两个回调函数改名connsck,看到https://stackoverflow.com/a/37379426/131929的细节

要获得更完整的发送示例,请看net.socket:send() in the docs。一旦你开始发送超过几个字节,这就变得相关了。

+0

非常感谢Marcel! – stas95

+0

谢谢!我错过了标题部分,导致了这个问题。 – satyadeepk

相关问题