我正在尝试将我的 Android 应用程序与 ESP8266 进行通信,但我没有收到 ESP 的响应。 我将 ESP 连接到我的 WI-FI 网络,这样我就可以通过电脑和手机上的浏览器进行访问,它工作正常,但当我尝试使用应用程序中的 Volley 进行访问时,没有任何反应。
ESP 响应浏览器访问,但使用 Volley 看起来它甚至没有收到请求,终端中没有任何反应。
这是代码
安卓:
package com.tests;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends AppCompatActivity {
private Button getReq;
private TextView respText;
private String url ="http://xxx.xxx.x.xx/request1/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getReq = (Button) findViewById(R.id.gtBt);
respText = (TextView) findViewById(R.id.resText);
getReq.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
respText.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
respText.setText("That didn't work!");
}
});
queue.add(stringRequest);
}
});
}
}
ESP8266:
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "xxxxxx"
#define STAPSK "xxxxxx"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
char bffWifi[20];
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(F("Connecting to "));
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
// Start the server
server.begin();
Serial.println(F("Server started"));
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println(F("new client"));
client.setTimeout(5000); // default is 1000
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(F("request: "));
Serial.println(req);
// Match the request
int val;
if (req.indexOf("/request1/") != -1) {
Serial.println("Request Received");
}
// Send the response to the client
// it is OK for multiple small client.print/write,
// because nagle algorithm will group them into one single packet
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
sprintf(bffWifi,"IT WORKS");
Serial.println(bffWifi);
client.println(bffWifi);
// The client will actually be *flushed* then disconnected
// when the function returns and 'client' object is destroyed (out-of-scope)
// flush = ensure written data are received by the other side
Serial.println(F("Disconnecting from client"));
// read/ignore the rest of the request
// do not client.flush(): it is for output only, see below
while (client.available()) {
// byte by byte is not very efficient
client.read();
}
}
最佳答案
为什么不尝试将您的 ESP 变成 Web 服务器并通过 http 请求(POST 和 GET)与其通信。
关于java - 使用 Volley 与 ESP8266 与 AndroidApp 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60855758/