-2

The application works when i have hardcoded the URL into ReadJSON().execute( my url here);

However after changing the url to textinput it will not load images.

This is the expected result: https://i.stack.imgur.com/d2WkR.jpg

Here is the entire file: https://pastebin.com/e6Q5ViuS

Here is the code that contains the error:

class ReadJSON extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            return readURL(params[0]);
        }

        @Override
        protected void onPostExecute(String content) {
            try {

                JSONArray jsonArray = new JSONArray(content);

                for(int i =0;i<jsonArray.length(); i++){
                    JSONObject productObject = jsonArray.getJSONObject(i);
                    arrayList.add(new Product(
                            productObject.getString("photo"),
                            productObject.getString("author")

                    ));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            CustomListAdapter adapter = new CustomListAdapter(
                    getApplicationContext(), R.layout.custom_list_layout, arrayList
            );
            lv.setAdapter(adapter);
        }
    }


    private static String readURL(String theUrl) {
        StringBuilder content = new StringBuilder();
        try {
            // create a url object
            URL url = new URL(theUrl);
            // create a urlconnection object
            URLConnection urlConnection = url.openConnection();
            // wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Jessica
  • 15
  • 3
  • 1
    Note that StackOverflow prefers your code to be in the post itself, instead of external links. External links might stop working in the future, which makes your question unclear for others. – gi097 Oct 22 '17 at 18:42

1 Answers1

0

The problem is here:

txtUrl.getText().toString();

You did not initialize txtUrl, which means that Android does not know what the txtUrl is. You can change it to this:

txtUrl = (EditText) findViewById(R.id.youredittextid);
EditTextValue = txtUrl.getText().toString();
gi097
  • 7,313
  • 3
  • 27
  • 49