Note that there are some explanatory texts on larger screens.

plurals
  1. PODownloading Tweets via JSON feed code not working
    text
    copied!<p>I'm building my first android app - something that will display my tweets in a list.</p> <p>I managed to get the app working earlier, but the tweets would sometimes take a long time to download, and the app would become unresponsive or crash. </p> <p>I decided to add a thread to the app so the it wouldn't become unresponsive, but now it doesn't work at all :/ </p> <p>Does anyone know what's wrong? It's only my third day of learning java, and I can't seem to figure out what the problem is here. </p> <p>Here's the code:</p> <pre><code>package com.app.first; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; 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 org.json.JSONObject; import android.app.ListActivity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; public class Twitter extends ListActivity { public ProgressDialog pd = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // Show the ProgressDialog on this thread pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false); new LoadTwitterFeed().execute(); } public class LoadTwitterFeed extends AsyncTask&lt;String, Integer, String&gt; { String tweets[] = new String[9]; public String readTwitterFeed() { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet( "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=jjmpsp&amp;include_rts=false&amp;count=10"); 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.e(ParseJSON.class.toString(), "Failed to download file"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return builder.toString(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String readTwitterFeed = readTwitterFeed(); try { JSONArray jsonArray = new JSONArray(readTwitterFeed); for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); tweets[i] = jsonObject.getString("text").toString(); } } catch (Exception e) { e.printStackTrace(); } setListAdapter(new ArrayAdapter&lt;String&gt;(Twitter.this, android.R.layout.simple_list_item_1, tweets)); return null; } @Override protected void onPostExecute(String result) { // what to do when the task ends. pd.hide(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload