Try with the debugger, maybe you can find a clue. On Aug 11, 2011 5:00 PM, "Anoop Namdev" <nam...@gowebbaby.com> wrote: > Hi, > I'm trying to retrieve twitter Json feed and display in list view. > I've created service to retrieve it. > When I call service in List activity service object gives null value. > > Service easily fetch data from twitter but service not able to return > data to client(List activity). > > In log cat no error occurred. What's the problem with my code? > > ParseJSON.java(service code) > > > > package gowebbaby.com.wp; > > import java.io.BufferedReader; > import java.io.IOException; > import java.io.InputStream; > import java.io.InputStreamReader; > import java.util.ArrayList; > import java.util.List; > import java.util.Timer; > import java.util.TimerTask; > > import org.apache.http.HttpEntity; > import org.apache.http.HttpResponse; > import org.apache.http.StatusLine; > import org.apache.http.client.ClientProtocolException; > import org.apache.http.client.HttpClient; > import org.apache.http.client.methods.HttpGet; > import org.apache.http.impl.client.DefaultHttpClient; > import org.json.JSONArray; > > import android.app.Service; > import android.content.Intent; > import android.os.Binder; > import android.os.IBinder; > import android.util.Log; > > > public class ParseJSON extends Service { > > public final static String KEY_ROWID = null; > private Timer timer = new Timer(); > private static final long UPDATE_INTERVAL = 50000; > private final IBinder mBinder = new MyBinder(); > private ArrayList<String> list = new ArrayList<String>(); > @Override > public void onCreate() { > super.onCreate(); > checkNewPosts(); > } > > private void checkNewPosts() { > timer.scheduleAtFixedRate(new TimerTask() { > @Override > public void run() { > String readWebservice = readWebservice(); > try { > JSONArray jsonArray = new JSONArray(readWebservice); > Log.i(ParseJSON.class.getName(), "Number of Post " + > jsonArray.length()); > > for(int i = 0 ; i < jsonArray.length(); i++) { > list.add(jsonArray.get(i).toString()); > //Log.i(ParseJSON.class.getName(), "Data " + > jsonArray.get(i).toString()); > } > > } catch (Exception e) { > e.printStackTrace(); > } > > } > },0, UPDATE_INTERVAL ); > > } > > protected String readWebservice() { > StringBuilder builder = new StringBuilder(); > HttpClient client = new DefaultHttpClient(); > HttpGet httpGet = new HttpGet("http://twitter.com/statuses/ > user_timeline/gowebbaby.json"); > > try { > HttpResponse response = client.execute(httpGet); > StatusLine statusLine = response.getStatusLine(); > int statusCode = statusLine.getStatusCode(); > > if(statusCode == 200) { > HttpEntity entity = response.getEntity(); > InputStream content = entity.getContent(); > BufferedReader reader = new BufferedReader(new > InputStreamReader(content)); > > String line; > while((line = reader.readLine()) != null) { > builder.append(line); > } > } else { > Log.i(ParseJSON.class.toString(), "Failed to download file"); > } > > } catch (ClientProtocolException e) { > e.printStackTrace(); > } catch (IOException e) { > e.printStackTrace(); > } > > return builder.toString(); > } > > @Override > public void onDestroy() { > super.onDestroy(); > if(timer != null) { > timer.cancel(); > } > Log.i(getClass().getSimpleName(), "Timer Stopped."); > } > > public List<String> getPostList() { > return list; > } > > public IBinder onBind(Intent intent) { > //Log.i(getClass().getSimpleName(), "onBind Called"); > return mBinder; > } > > public class MyBinder extends Binder { > ParseJSON getService() { > return ParseJSON.this; > } > } > } > > List Activity Code > > package gowebbaby.com.wp; > > import gowebbaby.com.wp.ParseJSON.MyBinder; > > import java.util.ArrayList; > import java.util.List; > > import android.app.ListActivity; > import android.content.ComponentName; > import android.content.Context; > import android.content.Intent; > import android.content.ServiceConnection; > import android.os.Bundle; > import android.os.IBinder; > import android.view.View; > import android.widget.ArrayAdapter; > import android.widget.ListView; > import android.widget.Toast; > > > public class WordpressPostList extends ListActivity { > /** Called when the activity is first created. */ > private ParseJSON s; > private ArrayList<String> values; > private ArrayAdapter<String> adapter; > public static final int POST_DETAIL = 1; > > @Override > public void onCreate(Bundle savedInstanceState) { > > super.onCreate(savedInstanceState); > setContentView(R.layout.post_list); > doBindService(); > //Log.i(WordpressPostList.class.getName(), "Post " + > s.getPostList().get(0)); > filldata(); > registerForContextMenu(getListView()); > } > > @Override > protected void onListItemClick(ListView l, View v, int position, long > id) { > super.onListItemClick(l, v, position, id); > Intent i = new Intent(this, WordpressPostDetail.class); > i.putExtra(ParseJSON.KEY_ROWID, id); > startActivityForResult(i, POST_DETAIL); > } > > private void filldata() { > if(s != null) { > List<String> postList = s.getPostList(); > values.clear(); > values.addAll(postList); > adapter = new > ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values); > adapter.notifyDataSetChanged(); > Toast.makeText(WordpressPostList.this, postList.get(0), > Toast.LENGTH_LONG).show(); > } > } > > void doBindService() { > bindService(new Intent(this, ParseJSON.class), mConnection, > Context.BIND_AUTO_CREATE); > } > > private ServiceConnection mConnection = new ServiceConnection() { > > @Override > public void onServiceConnected(ComponentName className, IBinder > service) { > MyBinder binder = (MyBinder) service; > s = binder.getService(); > //s = ((ParseJSON.MyBinder) service).getService(); > Toast.makeText(WordpressPostList.this, "connected", > Toast.LENGTH_LONG).show(); > } > > @Override > public void onServiceDisconnected(ComponentName className) { > s = null; > } > > > }; > } > > Here is Log cat messages. > > 08-11 19:18:35.772: DEBUG/AndroidRuntime(1526): >>>>>>>>>>>>>> > AndroidRuntime START <<<<<<<<<<<<<< > 08-11 19:18:35.781: DEBUG/AndroidRuntime(1526): CheckJNI is ON > 08-11 19:18:35.951: DEBUG/AndroidRuntime(1526): --- registering native > functions --- > 08-11 19:18:36.252: DEBUG/ddm-heap(1526): Got feature list request > 08-11 19:18:36.581: DEBUG/PackageParser(52): Scanning package: /data/ > app/vmdl30779.tmp > 08-11 19:18:36.671: INFO/PackageManager(52): Removing non-system > package:gowebbaby.com.wp > 08-11 19:18:36.671: DEBUG/PackageManager(52): Removing package > gowebbaby.com.wp > 08-11 19:18:36.671: DEBUG/PackageManager(52): Services: > gowebbaby.com.wp.ParseJSON > 08-11 19:18:36.671: DEBUG/PackageManager(52): Activities: > gowebbaby.com.wp.WordpressPostList > gowebbaby.com.wp.WordpressPostDetail > 08-11 19:18:36.751: DEBUG/PackageManager(52): Scanning package > gowebbaby.com.wp > 08-11 19:18:36.751: INFO/PackageManager(52): /data/app/vmdl30779.tmp > changed; unpacking > 08-11 19:18:36.762: DEBUG/installd(32): DexInv: --- BEGIN '/data/app/ > vmdl30779.tmp' --- > 08-11 19:18:37.001: DEBUG/dalvikvm(1532): DexOpt: load 37ms, verify > 108ms, opt 4ms > 08-11 19:18:37.011: DEBUG/installd(32): DexInv: --- END '/data/app/ > vmdl30779.tmp' (success) --- > 08-11 19:18:37.011: DEBUG/PackageManager(52): Services: > gowebbaby.com.wp.ParseJSON > 08-11 19:18:37.011: DEBUG/PackageManager(52): Activities: > gowebbaby.com.wp.WordpressPostList > gowebbaby.com.wp.WordpressPostDetail > 08-11 19:18:37.031: DEBUG/ActivityManager(52): Uninstalling process > gowebbaby.com.wp > 08-11 19:18:37.101: INFO/installd(32): move /data/dalvik-cache/ > data@a...@vmdl30779.tmp@classes.dex -> /data/dalvik-cache/ > data@a...@gowebbaby.com.wp.apk@classes.dex > 08-11 19:18:37.101: DEBUG/PackageManager(52): New package installed > in /data/app/gowebbaby.com.wp.apk > 08-11 19:18:37.201: DEBUG/AndroidRuntime(1526): Shutting down VM > 08-11 19:18:37.201: DEBUG/dalvikvm(1526): DestroyJavaVM waiting for > non-daemon threads to exit > 08-11 19:18:37.211: DEBUG/dalvikvm(1526): DestroyJavaVM shutting VM > down > 08-11 19:18:37.211: DEBUG/dalvikvm(1526): HeapWorker thread shutting > down > 08-11 19:18:37.211: DEBUG/dalvikvm(1526): HeapWorker thread has shut > down > 08-11 19:18:37.211: DEBUG/jdwp(1526): JDWP shutting down net... > 08-11 19:18:37.211: INFO/dalvikvm(1526): Debugger has detached; object > registry had 1 entries > 08-11 19:18:37.224: DEBUG/dalvikvm(1526): VM cleaning up > 08-11 19:18:37.231: DEBUG/dalvikvm(1526): LinearAlloc 0x0 used 623916 > of 5242880 (11%) > 08-11 19:18:37.261: INFO/dalvikvm(1526): JNI: AttachCurrentThread > (from ???.???) > 08-11 19:18:37.261: ERROR/AndroidRuntime(1526): ERROR: thread attach > failed > 08-11 19:18:37.261: DEBUG/ActivityManager(52): Uninstalling process > gowebbaby.com.wp > 08-11 19:18:37.362: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f0700e5 > 08-11 19:18:37.362: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f020031 > 08-11 19:18:37.362: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f020030 > 08-11 19:18:37.362: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f050000 > 08-11 19:18:37.381: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f060000 > 08-11 19:18:37.381: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f060001 > 08-11 19:18:37.582: DEBUG/dalvikvm(99): GC freed 137 objects / 5448 > bytes in 202ms > 08-11 19:18:37.721: DEBUG/dalvikvm(52): GC freed 9914 objects / 634248 > bytes in 220ms > 08-11 19:18:37.791: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f0700e5 > 08-11 19:18:37.791: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f020031 > 08-11 19:18:37.791: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f020030 > 08-11 19:18:37.791: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f050000 > 08-11 19:18:38.071: DEBUG/dalvikvm(52): GC freed 1469 objects / 66376 > bytes in 276ms > 08-11 19:18:38.082: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f060000 > 08-11 19:18:38.101: WARN/ResourceType(52): Resources don't contain > package for resource number 0x7f060001 > 08-11 19:18:38.521: DEBUG/AndroidRuntime(1537): >>>>>>>>>>>>>> > AndroidRuntime START <<<<<<<<<<<<<< > 08-11 19:18:38.521: DEBUG/AndroidRuntime(1537): CheckJNI is ON > 08-11 19:18:38.911: DEBUG/AndroidRuntime(1537): --- registering native > functions --- > 08-11 19:18:39.211: DEBUG/ddm-heap(1537): Got feature list request > 08-11 19:18:39.631: INFO/ActivityManager(52): Starting activity: > Intent { act=android.intent.action.MAIN > cat=[android.intent.category.LAUNCHER] flg=0x10000000 > cmp=gowebbaby.com.wp/.WordpressPostList } > 08-11 19:18:39.672: INFO/ActivityManager(52): Start proc > gowebbaby.com.wp for activity gowebbaby.com.wp/.WordpressPostList: > pid=1545 uid=10028 gids={3003} > 08-11 19:18:39.681: DEBUG/AndroidRuntime(1537): Shutting down VM > 08-11 19:18:39.691: DEBUG/dalvikvm(1537): DestroyJavaVM waiting for > non-daemon threads to exit > 08-11 19:18:39.691: DEBUG/dalvikvm(1537): DestroyJavaVM shutting VM > down > 08-11 19:18:39.691: DEBUG/dalvikvm(1537): HeapWorker thread shutting > down > 08-11 19:18:39.722: DEBUG/dalvikvm(1537): HeapWorker thread has shut > down > 08-11 19:18:39.722: ERROR/AndroidRuntime(1537): ERROR: thread attach > failed > 08-11 19:18:39.722: DEBUG/jdwp(1537): JDWP shutting down net... > 08-11 19:18:39.731: INFO/dalvikvm(1537): Debugger has detached; object > registry had 1 entries > 08-11 19:18:39.741: DEBUG/dalvikvm(1537): VM cleaning up > 08-11 19:18:39.771: DEBUG/dalvikvm(1537): LinearAlloc 0x0 used 639500 > of 5242880 (12%) > 08-11 19:18:39.901: DEBUG/ddm-heap(1545): Got feature list request > 08-11 19:18:40.531: INFO/NotificationService(52): enqueueToast > pkg=gowebbaby.com.wp callback=android.app.ITransientNotification$Stub > $Proxy@44d4f5e8 duration=1 > 08-11 19:18:40.641: INFO/ActivityManager(52): Displayed activity > gowebbaby.com.wp/.WordpressPostList: 979 ms (total 524702 ms) > 08-11 19:18:43.331: DEBUG/dalvikvm(1545): GC freed 3356 objects / > 233928 bytes in 68ms > 08-11 19:18:43.331: INFO/global(1545): Default buffer size used in > BufferedReader constructor. It would be better to be explicit if an 8k- > char buffer is required. > 08-11 19:18:43.451: DEBUG/dalvikvm(1545): GC freed 112 objects / > 295960 bytes in 60ms > 08-11 19:18:43.771: INFO/gowebbaby.com.wp.ParseJSON(1545): Number of > Post 20 > 08-11 19:18:43.892: DEBUG/dalvikvm(1545): GC freed 6310 objects / > 443456 bytes in 75ms > 08-11 19:18:44.271: DEBUG/dalvikvm(1545): GC freed 4978 objects / > 826048 bytes in 171ms > 08-11 19:18:45.931: DEBUG/dalvikvm(99): GC freed 2241 objects / 130240 > bytes in 135ms > 08-11 19:18:50.971: DEBUG/dalvikvm(225): GC freed 43 objects / 2056 > bytes in 138ms > > > > Thanks > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en
-- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en