I've created an apache server that I'm hosting on a Linux computer. On calling the URL it should return a JSON value. When I run the below code with my IP instead of the hosted webpage it returns a JSON value but when I put my hosted link URL (hosted with NO-IP) it returns the HTML that's behind the site. I don't know why I'm not getting JSON when I use the hosted URL instead of IP. Any suggestions?
import java.net. * ;
import java.io. * ;
public class UrlTest {
public static void main(String ar[]) {
try {
//below returns HTML
URL server = new URL("http://servername.ddns.net/test.php");
//below returns JSON val
//URL server = new URL("http://ip/test.php");
BufferedReader in =new BufferedReader(new InputStreamReader(server.openStream()));
String l;
while ((l = in.readLine()) != null)
System.out.println(l);
in.close();
} catch(Exception ex) {
}
}
}
Snippet of output with servername.ddns.net/test.php
:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<!-- WEB REDIRECTION PROVIDED BY NOIP.COM http://www.noip.com/ -->
<head>
<TITLE>Jaison Server</TITLE>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="icon" href="http://ip//favicon.ico" type="image/ico">
<link rel="shortcut icon" href="http://ip//favicon.ico">
</head>
<!--
<script language="JavaScript">
if(window != top) {
top.location.href = location.href;
}
</script>
-->
Expected output (also the output I get when I give the server IP):
{"chats":[{"text":"test text 1","sender":"Jaison Thomas"},{"text":"test text2","sender":"Sania Ejaz"}]}
Regions of code and output which had my IP or server URL have been replaced for security reasons.
as requested server code
<?php
$servername = "localhost";
$username = "jaison";
$password = "123456";
$dbname = "Chat";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM Text";
$result = $conn->query($sql);
$arraySQL = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arraySQL['chats'][] = $row;
}
echo json_encode($arraySQL);
} else {
echo "0 results";
}
$conn->close();
?>