0

Downloading content from a URL in Android but get a crash. Any idea what is going on?

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

editText.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after)  {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.i("TAG","text = "+editText.getText().toString());

        try {
            // Create a URL for the desired page
            URL url = new URL("http://www.omdbapi.com/?t=\" + editText.getText().toString() + \"&y=&plot=short&r=json");

            // Read all the text returned by the server
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                // str is one line of text; readLine() strips the newline character(s)
            }

            Log.d("alma", str);

            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }

    }
});
János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

0

Make sure you have proper permission for your application. If you are using internet, attach following line to Manifest file:

<uses-permission android:name="android.permission.INTERNET"/>
kTT
  • 1,340
  • 11
  • 19
  • Thanks, more detail here: http://stackoverflow.com/questions/2169294/how-to-add-manifest-permission-to-android-application – János Jun 14 '16 at 11:50