tried sending code to my android app but when i press get data, it shows Server Error 500, here is my ESP32 code, also the data shows on the web browser when i type the ip of my ESP32 in chrome
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <AsyncTCP.h>
const char* ssid = "ESP32-Access-Point";
const char* password = "YourPassword";
AsyncWebServer server(80);
JsonDocument jsonDoc;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
// Define routes using on
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
handleGetRequest(request);
});
server.begin();
}
void handleGetRequest(AsyncWebServerRequest *request) {
Serial.println("Received GET request");
jsonDoc["message"] = "Hello from ESP32!";
String jsonStr;
serializeJson(jsonDoc, jsonStr);
Serial.println("Sending JSON response: ");
Serial.println(jsonStr);
request->send(200, "application/json", jsonStr.c_str());
}
void loop() {
// Your code here
}
rnlcnls:
Server Error 500,
A 500 Internal Server error means that the website you were trying to connect to has experienced a problem and can't provide a more specific error code.
rnlcnls
February 10, 2024, 3:48am
3
is the problem on my ESP32 setup code or may be in my android studio code?
public void onClickGetData(View view) {
new Thread(() -> {
OkHttpClient client = new OkHttpClient();
String url = "http://192.168.4.1/json";
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
if (response.isSuccessful()) {
try {
assert response.body() != null;
SimpleTextMessage message = new Gson().fromJson(response.body().string(), SimpleTextMessage.class);
runOnUiThread(() -> {
String formattedMessage = getString(R.string.message_format, message.message);
tvShowData.setText(formattedMessage);
});
} catch (Exception e) {
handleErrorOnUiThread("Parsing error");
}
} else {
handleErrorOnUiThread("Server error: " + response.code());
}
}
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
handleErrorOnUiThread("Network error: " + e.getMessage());
}
});
}).start();
}
private void handleErrorOnUiThread(String message) {
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show());
}
private static class SimpleTextMessage {
String message;
}
}
this is my android studio code, used the latest ver of okhttp3 and implemented Gson, i also added the clearTextPermit to true
system
Closed
August 8, 2024, 3:48am
4
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.