I updated the code as following. However, now I see that the display
is not updated with the text. Am I missing something?
static String linkUrl = "http://www.google.com";
String dataText;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "OnCreate begins");
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_fetch);
new HttpConnect1().execute();
}
private class HttpConnect1 extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... nulls)
{
try {
URL connectURL = new URL(linkUrl);
HttpURLConnection conn = (HttpURLConnection)
connectURL.openConnection();
DataInputStream dis = new DataInputStream
(conn.getInputStream
());
byte[] data = new byte[1024];
int len = dis.read(data, 0, 1024);
dataText = new String(data, 0, len);
dis.close();
conn.disconnect();
}
catch(Exception e) {
Log.e(TAG, "Exception getting HTTPResponse");
return null;
}
return null;
}
@Override
protected void onPostExecute(Void nothing)
{
TextView bodyText = (TextView) findViewById
(R.id.android_fetchtext);
bodyText.setText(dataText);
}
}
On Oct 20, 1:16 pm, Mark Murphy <[email protected]> wrote:
> Kiran wrote:
> > Experts, Any help here?
> >> static String linkUrl = "http://www.google.com/";
>
> >> protected void onCreate(Bundle savedInstanceState) {
> >> super.onCreate(savedInstanceState);
>
> >> PowerManager pm = (PowerManager) getSystemService
> >> (Context.POWER_SERVICE);
> >> PowerManager.WakeLock wl = pm.newWakeLock
> >> (PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
> >> wl.acquire();
>
> Why are you asking for a WakeLock in an Activity's onCreate()?
>
>
>
>
>
> >> try {
> >> URL connectURL = new URL(linkUrl);
> >> HttpURLConnection conn = (HttpURLConnection)
> >> connectURL.openConnection();
> >> DataInputStream dis = new DataInputStream
> >> (conn.getInputStream
> >> ());
> >> byte[] data = new byte[1024];
> >> int len = dis.read(data, 0, 1024);
> >> dataText = new String(data, 0, len);
> >> }
> >> catch(Exception e)
> >> {
> >> Log.e(TAG, "Exception");
> >> return;
> >> }
>
> Never do HTTP I/O on the UI thread. Use AsyncTask or something to do
> this work on a background thread.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> Android App Developer Books:http://commonsware.com/books.html
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---