0

I'm trying to read in my android app JSON data from a website that returns JSON data with this code (in javascript):

 document.body.innerHTML = "<pre style='word-wrap: break-word; white-space: pre-wrap;'>" + JSON.stringify(address, null, 4) + "</pre>";

Example website can be found here: Link

The problem is that fetching this website from android would return me the full html source-code instead of giving me only the body with the JSON data.

The code in android to fetch this would look like this:

URL url = new URL(apiURL);
connection =  url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
    buffer.append(line+"\n");
    Log.d("Response: ", "> " + line);  
}

So I don't know how to manage this... I can modify both, the website html or the android app, but I've tried everything and I didn't find a solution.

Can someone help?

Xamortex
  • 1
  • 4
  • you can use jsoup to parse html – Anton Kazakov Nov 07 '17 at 22:16
  • is the website address returning JSON response like https://www.reddit.com/.json or only html content ?. – Popeye Nov 07 '17 at 22:22
  • @Popeye It returns JSON in the body (a normal user would see a JSON in the website) but if you check the source code it shows all the HTML code. When I use the .getInputStream function in android, it reads the HTML code insetad of reading the JSON delivered by the web page. Here you have an example: [link](http://glesports.eu/api/scripts.html) – Xamortex Nov 08 '17 at 10:25
  • See this would help https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/12980648/map-html-to-json&ved=0ahUKEwiR4r-pja_XAhVSySYKHXUyDQsQFggxMAE&usg=AOvVaw0Y-gQnYa3J9H3KwyGZF3ep. After converting it to Json, retrieve the value by the key of the content. – Popeye Nov 08 '17 at 13:43

1 Answers1

0

Try removing the unwanted HTML

URL url = new URL(apiURL);
connection =  url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
    buffer.append(line+"\n");
    Log.d("Response: ", "> " + line);  
}
// remove <pre> tag
line = line.replace("<pre style='word-wrap: break-word; white-space: pre-wrap;'>","").replace("</pre>","");
124697
  • 22,097
  • 68
  • 188
  • 315