Hello Everybody,
                            I am new in Android development, I used service
in my app to
                            get Httpconnection from  internet and read that
specific page in service.
                                              after reading the page, i am
using pattern matching to match
                           particular value from page
       while doing this, In service the if condition is not working that
means it does not enter in
      the if condition.
                 And also i have to display that data on Activity

     So please suggest me solution about to display content in activity(Code
is mentioned below)

Thanks in advance

*

package* example.demo;

*

import* java.net.URL;
*

import* java.net.URLConnection;
*

import* java.util.regex.Matcher;
*

import* java.util.regex.Pattern;

*

import* android.app.Service;
*

import* android.content.Intent;

*

import* android.os.IBinder;
*

import* android.util.Log;
*

import* android.widget.Toast;

*

public* *class* Mysevice *extends* Service {

*protected* *static* *final* String *TAG* = "MyService";

*public* *static* *final* *int* *MAX_VALUE*=100;

//public static byte[]readData=new byte[MAX_VALUE];

*public* *static* String *out*=*null*;

*public* *static* String *data* ,*readData* ;

@Override

*public* IBinder onBind(Intent intent) {

*return* *null*;

}

@Override

*public* *void* onCreate() {

Toast.*makeText*(*this*, "My Service Created", Toast.*LENGTH_LONG*).show();

Log.*d*(*TAG*, "onCreate");

 }

@Override

*public* *void* onDestroy() {

Toast.*makeText*(*this*, "My Service Stopped", Toast.*LENGTH_LONG*).show();

Log.*d*(*TAG*, "onDestroy");

}

@Override

*public* *void* onStart(Intent intent, *int* startid) {

*try
*

{

*int* connectTimeout = 10;

*int* readTimeout = 20;

URL url = *new* URL("http://money.rediff.com/";);

URLConnection urlConn = (URLConnection) url.openConnection();

urlConn.setDoOutput(*true*);

urlConn.setDoInput(*true*);

urlConn.setDoOutput(*true*); // 20 Seconds Connect Timeout

urlConn.setConnectTimeout(connectTimeout * 1000); // 20 Minutes ReadTimeout

urlConn.setReadTimeout(readTimeout * 1000);

*try*{

urlConn.connect();

 *out* = *new*StreamToString().convertStreamToString(urlConn.getInputStream());

Toast.*makeText*(*this*, "Reading the page", Toast.*LENGTH_LONG*).show();

Log.*d*(*TAG*, "onStart");

}*catch*(java.net.SocketTimeoutException ex)

{

String msg = "Could not able to establish the connection between ur machine
and money.rediff.com";

System.*out*.println(msg);

}

String regex = "<div class=\"floatR\"><span class=\"f20 bold\"[^>]*>"

+".*?"

+"([^\"]+)</span>";

Pattern p = Pattern.*compile*(regex);

Matcher m = p.matcher(*out*);

 *while*(m.find())

{

*readData* = m.group(1);

// ByteArrayInputStream *baos* = new ByteArrayInputStream(readData);

// data=baos.toString();

Toast.*makeText*(*this*, "Pattern has matched", Toast.*LENGTH_LONG*).show();

}

Toast.*makeText*(*this*, "Match not found", Toast.*LENGTH_LONG*).show();

 } *catch* (Exception ex) {

ex.printStackTrace();

*throw* *new* RuntimeException("Exception occured in
testDownApplicationTurnedGreenByTraffic", ex);

}

Toast.*makeText*(*this*, "My Service Started", Toast.*LENGTH_LONG*).show();

Log.*d*(*TAG*, "onStart");

}

}

-- 
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

Reply via email to