1

First off, yes, I have done research on this question. And yes, I have found an answer here. But the whole process still isn't working for me. All I need to do is grab text off of a webpage like Google, and create a string from the text it grabs. Here is my code with the aforementioned tutorials code in it:

public class Searching_Animation_Screen extends ActionBarActivity {
TextView loading_txt;
Animation blink;
public String pre_split;
public String[] split_string;
TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchinganimationscreen);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    int width = getWindowManager().getDefaultDisplay().getWidth();
    loading_txt = (TextView)findViewById(R.id.loading);
    text =(TextView)findViewById(R.id.textView);
    Typeface pacifico_typeface = Typeface.createFromAsset(getAssets(), "fonts/pacifico.ttf");
   loading_txt.setTypeface(pacifico_typeface);
   loading_txt.setTextSize(width / 20);
   blink = AnimationUtils.loadAnimation(getApplicationContext(),
           R.anim.blink);
   loading_txt.setAnimation(blink);
   Begin();

}

private void Begin() {
    Intent SEARCH_INTENT = getIntent();
    pre_split=SEARCH_INTENT.getStringExtra("Search_Text");
    split_string = pre_split.split(" ");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_searchinganimationscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    String google_url ="https://www.google.com/#safe=active&q=";

    @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) {
        text.setText(Html.fromHtml(result));
        //throw into summarizer
    }


    public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] {"www.google.com"});

    }
}

}

Android studio is saying that readWebpage is never used, along with the actual DownloadWebPageTask class. Any ideas? I would like this class to run immediately on Create. Thanks!

Community
  • 1
  • 1
Ethan
  • 175
  • 2
  • 11
  • you are not calling readWebpage from anywhere, are you suppose to call it in the onCreate method, if so, add readWebpage there – faljbour Feb 15 '15 at 23:46
  • @faljbour can you please post an answer with the changes you are proposing? – Ethan Feb 15 '15 at 23:55

1 Answers1

1

@Ethan, sure, I hope this is what you want, just adding the readWebpage method in the onCreate method, but I modified it and removed the View object since it is not being used,

    public class Searching_Animation_Screen extends ActionBarActivity {
TextView loading_txt;
Animation blink;
public String pre_split;
public String[] split_string;
TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchinganimationscreen);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    int width = getWindowManager().getDefaultDisplay().getWidth();
    loading_txt = (TextView)findViewById(R.id.loading);
    text =(TextView)findViewById(R.id.textView);
    Typeface pacifico_typeface = Typeface.createFromAsset(getAssets(), "fonts/pacifico.ttf");
   loading_txt.setTypeface(pacifico_typeface);
   loading_txt.setTextSize(width / 20);
   blink = AnimationUtils.loadAnimation(getApplicationContext(),
           R.anim.blink);
   loading_txt.setAnimation(blink);
   Begin();

  //* call webpage here, 
  //* note, i removed passing the view object since it is not being used
  readWebpage()

}

 //* (modify) by remvoving it from the code below 
 //* and removing the view object since it is not being used
 public void readWebpage() {
     DownloadWebPageTask task = new DownloadWebPageTask();
     task.execute(new String[] {"http://www.google.com"});

 }

private void Begin() {
    Intent SEARCH_INTENT = getIntent();
    pre_split=SEARCH_INTENT.getStringExtra("Search_Text");
    split_string = pre_split.split(" ");
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_searchinganimationscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    String google_url ="https://www.google.com/#safe=active&q=";

    @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) {
        text.setText(Html.fromHtml(result));
        //throw into summarizer
    }

  }

}
faljbour
  • 1,367
  • 2
  • 8
  • 15
  • Thank you for your answer, but it doesn't totally work. In the code I posted, readWebpage was in a totally separate class called `DownloadWebPageTask` and therefore cannot be accessed the way you suggested above. Also, the `DownloadWebPageTask` class isn't being called at all. Any ideas how to fix these issues? – Ethan Feb 16 '15 at 00:13
  • looking at your code again, it seems the readWebpage() method is inside the of the DownloadWebpageTask class, you would need to remove it and put it outside of the class, let me rewrite the answer and arrange the code to see if that fixes the problem. – faljbour Feb 16 '15 at 00:21
  • check out the update answer and where the readwebpage method was moved – faljbour Feb 16 '15 at 00:25
  • Thank you for all your help so far. The code you gave me is clean, but I am still not getting any text back from google. Any ideas? – Ethan Feb 16 '15 at 01:35
  • Ethan, I would recommend that you try to learn how debug an android app, it is easy, I use eclipse and and I can put a break point and see what is going on in the code, on of the items that was needed is to add use-permission "android.permission.INTERNET" and add http:// to your url. I tested the code on a test app and worked just fine after the two changes I made – faljbour Feb 16 '15 at 03:14
  • It never occurred to me to include either of those changes. Thanks! – Ethan Feb 16 '15 at 03:36