Home > Software design >  Firebase REST API Java(Not android). I got HTML Texts instead of my database value
Firebase REST API Java(Not android). I got HTML Texts instead of my database value

Time:02-24

I was making my new REST API Java project with Firebase. I ran my code here:

URL url = new URL("https://restapi-example-java-default-rtdb.asia-southeast1.firebasedatabase.app/get");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type","application/json");
conn.setRequestProperty("Accept","application/json");
conn.setConnectTimeout(5000);
conn.setDoInput(true);

conn.connect();
System.out.println(conn.getResponseMessage());
int responseCode = conn.getResponseCode();
System.out.println(responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((inputLine = in.readLine()) != null) {
    outResult.append(inputLine);
}

System.out.println(outResult);

However, what I could get was some kind of html which was about Google Login. Response Message and Code of the connection was OK and 200;

My firebase rule:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

CodePudding user response:

To read from the REST API of the Realtime Database, the URL must end in .json. So:

new URL("https://restapi-example-java-default-rtdb.asia-southeast1.firebasedatabase.app/get.json");
                                                                                          //            
  • Related