Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:40:16+00:00 2026-05-31T02:40:16+00:00

Hi Having an activity that loads a twitter feed. Android Twitter String to Json

  • 0

Hi Having an activity that loads a twitter feed.
Android Twitter String to Json Array.

I’m wanting to put a refresh button at the top that when clicked reloads the data.

so far i’ve got it to just reload the activity is this the best way of doing this?

    package co.uk.fantasticmedia.TheEvoStikLeague;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



import android.app.ListActivity;



public class TwitterActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    setContentView(R.layout.twitteract);


    Button refresh = (Button) findViewById(R.id.btn_refresh);

    //Listening to button event
    refresh.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            Intent reload = new Intent(getApplicationContext(), TwitterActivity.class);



            startActivity(reload);


        }
    });





     try{
            // Create a new HTTP Client
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            // Setup the get request
            HttpGet httpGetRequest = new HttpGet("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");

            // Execute the request in the client
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            // Grab the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();

            Log.v(json,"jsonfeed");




            List<String> items = new ArrayList<String>();

              //items.add(json);


            JSONArray jArray = new JSONArray(json);


            for (int i=0; i < jArray.length(); i++)
            {    JSONObject oneObject = jArray.getJSONObject(i);
                items.add(oneObject.getString("text"));
                 Log.i("items", "items");
            }

            setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
            ListView list = getListView();
            list.setTextFilterEnabled(true);


            list.setOnItemClickListener(new OnItemClickListener(){

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
                }



            });


        } catch(Exception e){
            // In your production code handle any errors and catch the individual exceptions
            e.printStackTrace();
        }

    }

















    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.home) {

            startActivity(new Intent(TwitterActivity.this, HomeActivity.class));

            return(true);
      }


        if (item.getItemId() == R.id.match) {

            startActivity(new Intent(TwitterActivity.this, MatchActivity.class));

            return(true);
      }



        if (item.getItemId() == R.id.teams) {

            startActivity(new Intent(TwitterActivity.this, TeamsActivity.class));

            return(true);
      }



        if (item.getItemId() == R.id.twitter) {

            startActivity(new Intent(TwitterActivity.this, TwitterActivity.class));

            return(true);
      }

        if (item.getItemId() == R.id.info) {

            startActivity(new Intent(TwitterActivity.this, InfoActivity.class));

            return(true);
      }


        return(super.onOptionsItemSelected(item));


    }




}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:40:18+00:00Added an answer on May 31, 2026 at 2:40 am

    can you post the rest of your activity? Especially the portion that you are using to load the list the first time.

    Probably it is unnecessary to restart the entire activity. You just need to move the portion of your code that populates the list for you into its own method. Then you can call that method when the user wants to refresh.

    If nothing else you should change:

     Intent reload = new Intent(getApplicationContext(), TwitterActivity.class);
    

    to

    Intent reload = new Intent(TwitterActivity.this, TwitterActivity.class);
    

    EDIT:
    You need to move your try / catch block into a new method called refresh(). Then call that method anytime you want to reload the list. like this:

    It would also be a bood idea to move your networking out of the main thread.

    public class TwitterActivity extends ListActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.twitteract);
    
    
        Button refresh = (Button) findViewById(R.id.btn_refresh);
    
        //Listening to button event
        refresh.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0) {
                refresh();
    
    
            }
        });
    
    
    
        refresh();
    
    
    
    }
    
    private void refresh(){
    
         try{
            // Create a new HTTP Client
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            // Setup the get request
            HttpGet httpGetRequest = new HttpGet("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");
    
            // Execute the request in the client
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            // Grab the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
    
            Log.v(json,"jsonfeed");
    
    
    
    
            List<String> items = new ArrayList<String>();
    
              //items.add(json);
    
    
            JSONArray jArray = new JSONArray(json);
    
    
            for (int i=0; i < jArray.length(); i++)
            {    JSONObject oneObject = jArray.getJSONObject(i);
                items.add(oneObject.getString("text"));
                 Log.i("items", "items");
            }
    
            setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
            ListView list = getListView();
            list.setTextFilterEnabled(true);
    
    
            list.setOnItemClickListener(new OnItemClickListener(){
    
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
                }
    
    
    
            });
    
    
        } catch(Exception e){
            // In your production code handle any errors and catch the individual exceptions
            e.printStackTrace();
        }
    
    
    
    
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        if (item.getItemId() == R.id.home) {
    
            startActivity(new Intent(TwitterActivity.this, HomeActivity.class));
    
            return(true);
      }
    
    
        if (item.getItemId() == R.id.match) {
    
            startActivity(new Intent(TwitterActivity.this, MatchActivity.class));
    
            return(true);
      }
    
    
    
        if (item.getItemId() == R.id.teams) {
    
            startActivity(new Intent(TwitterActivity.this, TeamsActivity.class));
    
            return(true);
      }
    
    
    
        if (item.getItemId() == R.id.twitter) {
    
            startActivity(new Intent(TwitterActivity.this, TwitterActivity.class));
    
            return(true);
      }
    
        if (item.getItemId() == R.id.info) {
    
            startActivity(new Intent(TwitterActivity.this, InfoActivity.class));
    
            return(true);
      }
    
    
        return(super.onOptionsItemSelected(item));
    
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application i am having checkboxes in first activity that i want to
I'm having a problem with an android application that I'm working on. My application
Hi i am having a activity with a button, on click of the button
I am having trouble getting my activity to generate a MotionEvent.ACTION_UP. Probably a beginner's
I am having trouble with my activity management. So I have activity A which
I'm having difficulties handling multiple instances of my root (main) activity for my application.
I'm having a rather odd issue where the menu for an activity works totally
I am having trouble writing a query on a mysql table of user activity
So what I am trying to create is an activity that displays an image
I am having a Default.png file in my project, so that whenever my app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.