I want MoviePage to display JSON data from a website when the relevant item in ListView is clicked.
Here is the code from my ListView page that opens a new activity when the item is clicked:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String movieData = ((TextView) view).getText().toString();
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putExtra("title", movieData);
startActivity(newActivity);
}
});
and here is the code of the page in which I wish to display the JSON data on
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String keyword = getIntent().getExtras().getString("keyword");
keyword = keyword.replaceAll("\\s", "+");
Intent newActivity1 = new Intent();
setResult(RESULT_OK, newActivity1);
private String movieData(String keyword) {
String jsonResult = null;
try {
URL url = new URL("https://omdbapi.com/?t=" + keyword);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream input = httpURLConnection.getInputStream();
Scanner scanner = new Scanner(input, "UTF-8").useDelimiter("\\A");
jsonResult = scanner.hasNext() ? scanner.next() : "";
} catch (Exception e) {
e.printStackTrace();
}
return jsonResult;
}