1

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;
}
pt89
  • 55
  • 8

2 Answers2

0
String keyword = getIntent().getExtras().getString("keyword");

Shouldn't this be,

String keyword = getIntent().getExtras().getString("title");

You should define your keys as constants so you don't make mistakes like this.

public static final String EXTRA_TITLE = "title";

newActivity.putExtra(EXTRA_TITLE, movieData);

Then when you take it out

getIntent().getExtras().getString(ClassWhereIDefinedMyKey.EXTRA_TITLE);
Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
0

Instead store json object into sharedpreferences so you can retrive the object in any class of your application.You need to convert that object into string first and then store into sharedpreferences and vice versa for retriving link

Community
  • 1
  • 1
Dhiraj
  • 870
  • 2
  • 12
  • 25