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 8955893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:39:29+00:00 2026-06-15T14:39:29+00:00

I was reading about AsyncTask and I tried a program. But it does not

  • 0

I was reading about AsyncTask and I tried a program. But it does not seem to work. I am new to programming, if possible can you please help me out.

public class MainActivity extends Activity {

TextView title;
TextView date;
TextView cat;
TextView desc;
Button rss;

difficultTask doBack;

private static final String TAG_ITEM = "item";
private static final String TAG_TITLE = "title";
private static final String TAG_LINK = "link";
private static final String TAG_DESC = "description";
private static final String TAG_DATE = "pubDate";
private static final String TAG_CAT = "category";


JSONArray rssFeed = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);           
    title = (TextView)findViewById(R.id.title);                  
    date = (TextView)findViewById(R.id.date);        
    cat = (TextView)findViewById(R.id.cat);        
    desc = (TextView)findViewById(R.id.description);        
    rss = (Button)findViewById(R.id.rss);
    rss.setOnClickListener(new Button.OnClickListener() {            
        @Override
        public void onClick(View v) {
            doBack = new difficultTask();
            doBack.execute();                
            }
        });        
}

class difficultTask extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {        

        JSONObject json = getJSONFromUrl("http://example.com/mysite/rss.js");

        String categ = "";String t = "";String d = "";String da = "";String a = ""; 

        try {    
            JSONArray jsonArray = json.getJSONArray(TAG_ITEM);
            for(int i = 0; i<jsonArray.length(); i++){
                JSONObject c = rssFeed.getJSONObject(i);
                 t = c.getString(TAG_TITLE);
                 a = c.getString(TAG_LINK);
                 d = c.getString(TAG_DESC);
                 da = c.getString(TAG_DATE);
                 categ = c.getString(TAG_CAT);

            }

            Log.d("Log", t+"/n"+a+"/n"+d+"/n"+da+"/n"+categ);
        } catch (JSONException e) {
            Log.d("Log", e.getMessage());
            e.printStackTrace();
        }            
        Log.d("Log", "Background");
        return t;
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      title.setText(result);
    }

    public JSONObject getJSONFromUrl(String url){
        InputStream ips = null;
        JSONObject jsonObj = null;
        String json = "";

        try{
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);               
            HttpResponse respone = httpClient.execute(httppost);
            HttpEntity entity = respone.getEntity();

            ips = entity.getContent();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = bufff.readLine()) != null){
                sb.append(line + "\n");
            }
            ips.close();
            json = sb.toString();
        }
        catch(Exception e){
            Log.d("Log", e.toString());
        }
            try {
                jsonObj = new JSONObject(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        return jsonObj;        
    }


}

}

doInBackground() is not working, please help.
And how i can return some Strings t,a,c,d,da from background?

UPD: Sorry Logcat not worked, now i see error’s

12-01 19:17:30.109: E/AndroidRuntime(2500): FATAL EXCEPTION: AsyncTask #1
12-01 19:17:30.109: E/AndroidRuntime(2500): java.lang.RuntimeException: An error occured while executing doInBackground()
12-01 19:17:30.109: E/AndroidRuntime(2500):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.lang.Thread.run(Thread.java:856)
12-01 19:17:30.109: E/AndroidRuntime(2500): Caused by: java.lang.NullPointerException
12-01 19:17:30.109: E/AndroidRuntime(2500):     at com.example.mytwitter.MainActivity$difficultTask.doInBackground(MainActivity.java:99)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at com.example.mytwitter.MainActivity$difficultTask.doInBackground(MainActivity.java:1)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-01 19:17:30.109: E/AndroidRuntime(2500):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-01 19:17:30.109: E/AndroidRuntime(2500):     ... 4 more
  • 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-06-15T14:39:32+00:00Added an answer on June 15, 2026 at 2:39 pm

    Okay I’ve added some simple Asynctask for you..try and tell me whether this working to you.

    public class SenderActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            SenderActivity.Sender.execute();
        }
    
        class Sender extends AsyncTask<Void, Integer, Integer> {
            ProgressDialog pd = null;
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pd = new ProgressDialog(SenderActivity.this);
                pd.setTitle("Sending...");
                pd.setMessage("Please wait..");
                pd.setCancelable(false);
                pd.show();
            }
            @Override
            protected Integer doInBackground(Void... params) {
                System.out.println("Try some simple operation to learn well");
                //check whether this printing in your logcat
                return 3;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);
                pd.dismiss();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got some concerns about AsyncTask that I could not clarify when reading the
While reading about memmove I read that it can handle MEMORY OVERLAPS but I
When reading about pipes in Advanced Programming in the UNIX Environment, I noticed that
I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android.
After reading about Mapper XMLs I can't help to wonder how one might go
While reading about wireless technology, I usually meet the term provisioning. Anyone can help
In reading about SCM (software configuration mgt, not supply chain mgt), I have seen
After reading about both, I just have curiosity, how programming community uses this? In
After reading a few posts and reading on the developers page about ASYNCTASK, I
After reading about Visual Studio 11's Portable Libraries, I tried putting my common library

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.