我一直在尝试使用ESP32中的SoftAP和Web服务器以及android中的Volley Library将ESP32连接到我的Android应用。我可以从网络浏览器连接到ESP32网络服务器。但是我无法通过android volley访问该网页。我在截击中收到空错误响应。
我的手机能够连接到SoftAP。
这是服务器的ESP32代码:
void setup()
{
pinMode(pin_led, OUTPUT);
Serial.begin(115200);
SPIFFS.begin();
wifiConnect();
server.on("/l",[](){server.send_P(200,"text/html", webpage);});
server.on("/toggle",toggleLED);
server.on("/settings", HTTP_POST, handleSettingsUpdate);
server.begin();
}
这是我的排球要求:
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url ="http://192.168.4.1/l";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.d("Connection", "Response: "+response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try{
Log.d("Connection", error.networkResponse.toString());
} catch (Exception e){
Log.d("Connection", e.toString());
}
}
});
// Add the request to the RequestQueue.
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
似乎是因为Oreo默认未启用“明文流量”。一旦我在清单中添加了以下代码,它就开始工作。
<activity android:name=".MainActivity" android:usesCleartextTraffic="true">
该解决方案可从this StackOverflow答案的类似问题中找到。