Reputation: 73
I am currently in a curious situation: my goal is to get the html
of a website and convert it into a String
so that I can essentially read what is on the website. However, my code returns an error; I have isolated the line of code that contains the error, but I am unable to make it work. As shown in the error log below, the type of cast is incorrect. Does anyone know how to fix it? Thanks!
public class FirstDisplay extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_display);
Button button1 = (Button) findViewById(R.id.deliver);
final TextView textview1 = (TextView) findViewById(R.id.headline);
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) {
//textview1.setText(Html.fromHtml(result));
}
}
button1.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick (View v) {
DownloadWebPageTask task = new DownloadWebPageTask();
AsyncTask<String, Void, String> headline = task.execute(new String[] { "http://www.google.com" });
//HERE IS THE LINE OF CODE THAT CONTAINS THE ERROR
textview1.setText((CharSequence) headline);
}
});
}
}
Here is the error log:
07-25 18:14:42.359: W/dalvikvm(9268): threadid=1: thread exiting with uncaught exception (group=0x416dfda0)
07-25 18:14:42.359: E/AndroidRuntime(9268): FATAL EXCEPTION: main
07-25 18:14:42.359: E/AndroidRuntime(9268): Process: com.app.myfirstapp, PID: 9268
07-25 18:14:42.359: E/AndroidRuntime(9268): java.lang.ClassCastException: com.app.myfirstapp.MainDisplay$1DownloadWebPageTask cannot be cast to java.lang.CharSequence
Upvotes: 1
Views: 46
Reputation: 93688
Your problem is here:
textview1.setText((CharSequence) headline);
Headline is an async task, not a string (or other charsequence). Delete this line, and set the text in onPostExecute
Upvotes: 1
Reputation: 149
This is some really old code of mine, but I think it used to work:
static class scrapeIt extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(urls[0]);
HttpResponse response;
try {
response = httpClient.execute(get);
String bodyHtml = EntityUtils.toString(response.getEntity());
return bodyHtml;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Internal-Error: #0237";
}
}
I believe your issue lies in trying to cast an AsyncTask to a "string" which is not allowed, what you want to do is get the string generated by the AsyncTask and work with that.
Upvotes: 0