0

I can't figure out how to make this code work in an AsyncTask, I searched for multiple examples but it keeps crashing. I found this simple code on the internet and I want to adapt it to get the URL from a textfield and get the HTML code. I found out it has to be in an AsyncTask otherwise it won't work but even in an AsyncTask I can't get it to work. Here's my code:

String ETURL = ETURLInput.getText().toString();

try {
  URL TestURL = new URL(ETURL);

  BufferedReader bufferReader = new BufferedReader(
                    new InputStreamReader(TestURL.openStream()));

  String outputCode;
  while ((outputCode = bufferReader.readLine()) != null)
    TVCode.setText(outputCode);
    bufferReader.close();
} catch (Exception e) {
  TVCode.setText("Oops, something went wrong.")

}

}

This is the code which needs to be executed inside an ActionListener. So when I click the button it should execute this code in an AsyncTask.

Hopefully somebody could help me with this.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
JI38D
  • 11
  • 2
  • As a guess, your `AsyncTask` is crashing because you are trying to update the UI (via your `setText` calls) from `doInBackground`. Your error from `LogCat`, and the code for your attempt at moving this code to an `AsyncTask` will help. – PPartisan Oct 06 '15 at 16:55
  • @PPartisan Thank you for your answer. I tried this and I get the following errors: Caused by: java.net.MalformedURLException: Protocol not found:,,,, java.lang.RuntimeException: An error occured while executing doInBackground(),,,,, java.lang.SecurityException: Permission denied (missing INTERNET permission?). I have the Internet permission correctly used so I have no idea why it gives this error. – JI38D Oct 06 '15 at 17:20
  • In that case, have you seen the other answers relating to this error message? I.e. [Permission denied (missing INTERNET permission?)](http://stackoverflow.com/questions/25135595/permission-denied-missing-internet-permission) – PPartisan Oct 06 '15 at 17:28
  • It still isn't working. I tried that before. – JI38D Oct 06 '15 at 17:41
  • Best to update your question here instead, as any conversations we have via email can't benefit anyone else. Update your code with your AsyncTask implementation - however, my suggestions from before, even though they may still be valid, wont relate to your error. – PPartisan Oct 06 '15 at 18:50

1 Answers1

0

You forgot to add openConnection, add this: URLConnection conn = TestURL.openConnection(); after creating your URL object.

To make it work with an asynctask, what you can do is storing your string in a class variable, returning it in the doInBackGround and using it in your onPostExecute.

An example of method you can create in your asynctask:

protected String getContentUrl(String URL) {
  String line=null;
  String result="";
  try {
    try {
      URL url;
      // get URL content
      url = new URL(URL);
      URLConnection conn = url.openConnection();

      // open the stream and put it into BufferedReader
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      line=br.readLine();
      while (line!= null) {
        result=result+line;
        line=br.readLine();
      }
      //System.out.print(result);
      br.close();

      return result;
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

  return null;

Then you get your result this way on doInBackGround:

getContentUrl(YOUR URL HERE)

Store this value in a String, and return it. Then you can use it in your onPostExecute

Hope it helps :)

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Virthuss
  • 3,142
  • 1
  • 21
  • 39