-1

I'm trying to write my first Android application and need to get a webpage's text as string to display it in a TextView. I found a few samples on StackOverflow but none of them seems to work for me for some reason. When I click the button to retrieve the text the app crashes. Here's what I've got now (based on the code from Get text from web page to string):

The MainActivity.java file



    public class MainActivity extends ActionBarActivity {

      Button testbutton;
      Button btnReset;
      TextView serverMsgViewComponent;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          addListenerOnButton();
      }

      public void addListenerOnButton() {
          testbutton = (Button) findViewById(R.id.btnClickme);
          btnReset = (Button) findViewById(R.id.btnResetText);
          serverMsgViewComponent = (TextView) findViewById(R.id.serverMsgView);
          serverMsgViewComponent.setText("Custom text");
          final ReadWebpageAsyncTask readpage = new ReadWebpageAsyncTask();
          btnReset.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  serverMsgViewComponent.setText("Server message placeholder");
              }
          });
          testbutton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  serverMsgViewComponent.setText("Retrieving message from server...");
                  readpage.readWebpage();
              }

          });

      }

      //some default code here
    }

And ReadWebpageAsyncTask.java



public class ReadWebpageAsyncTask extends Activity {
    private TextView textView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.serverMsgView);
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            textView.setText(Html.fromHtml(result));
        }
    }

    public void readWebpage() {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://hglstudio.com/workspace/server.htm" });

    }
}

Community
  • 1
  • 1
Val
  • 1,548
  • 1
  • 20
  • 36

1 Answers1

1

So, I said I copied code from another SO discussion and this code didn't work for me and I needed help figuring out why. Instead I got my question marked as a duplicate of the other one from which I took my code and my question was also downvoted. Thank you, StackOverflow!

Now, I found the problem myself (happily!). And problem was with the domain name which was not getting resolved to the IP address for some reason and then threw an error. The solution was to first "initialize" the domain by accessing the url once, and then try downloading the text in the second attempt.

So I'm calling this function first:

private void initializeDns(String url) {
    try {
        InetAddress address = InetAddress.getByName(url);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}
Val
  • 1,548
  • 1
  • 20
  • 36