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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:19:38+00:00 2026-06-04T14:19:38+00:00

I am working on an android app. And it has to read in some

  • 0

I am working on an android app. And it has to read in some feed from the internet based on json. Well i’ve figured that part out by now. But when I test my App, the app gets stuck on the part where it’s downloading the feed. The UI get’s stuck for a couple of seconds.

I’ve figurred out already that I need to make use of the AsyncTask class in android to run the connection on the background. I’ve read so many on this topic, that I can almost dream the theory. Now to put it in practice, it gives me a little bit of a problem.

The app has a bunch of classes now, but the class(activity) that handles the downloading of the feed and puts the retreived data into a listView. the class is called KVONieuws ( Dutch for KVO – NEWS ) here’s the source:

package com.appsoweb.kvodeventer;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.appsoweb.kvodeventer.JSONfunctions;
import com.appsoweb.kvodeventer.KVONieuws;
import com.appsoweb.kvodeventer.R;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class KVONieuws extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        JSONObject json = JSONfunctions.getJSONfromURL("http://crossalertdeventer.nl/api/news.json");

        try{

            JSONArray  earthquakes = json.getJSONArray("items");

            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = earthquakes.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Titel:" + e.getString("title"));
                map.put("image", "Image: " +  e.getString("image"));
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Parsing error "+e.toString());
        }


        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.singlelistitem, 
                        new String[] { "name", "image" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(KVONieuws.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }
}

Now I have created another class Called JSONfunctions.java here that handles the JSON part of the story:

package com.appsoweb.kvodeventer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                httppost.setHeader("User-Agent", "9fb01091b51527555d1d3fc87709918f");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

Now i’ve tried so much with this code to make the (heavy part) run as a thread on the background, and I understand the theory of it, I just can’t get it to work. And I don’t know WHY….

Is there anybody out there that can maybe simply adjust my code or give pointers to where to implement an inner class maybe to extend the AsyncTask class, and to give an idea about where to put what code in the run in background method and the onpostexecute…

Thanks to anyone that will help me!

  • 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-04T14:19:40+00:00Added an answer on June 4, 2026 at 2:19 pm

    call new MyRssReadTask().execute(); in onCreate Method…

    See this Link

      class MyRssReadTask extends AsyncTask<Void, Void, JSONObject> {
            ProgressDialog waitingDialog;
            @Override
            protected void onPreExecute() {
                waitingDialog = new ProgressDialog(KVONieuws.this);
                waitingDialog.setMessage("Loading...");
                waitingDialog.show();
                super.onPreExecute();
            }
    
            @Override
            protected Void doInBackground(Void... unused) {
    
               JSONObject json = JSONfunctions.getJSONfromURL("http://crossalertdeventer.nl/api/news.json");
    
                return json;
            }
    
            @Override
            protected void onPostExecute(JSONObject objJson) {
                super.onPostExecute(result);
                if(waitingDialog.isshowing()){  
                waitingDialog.dismiss();}
    
               //do stuff here 
                JSONArray  earthquakes = objJson.getJSONArray("items");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an android app that has a database in which one
I have an app that is available from the Android Market. Some users have
I am working on an Android app that has multiple screens the user will
I am working on an android app that has two activities. One is the
I'm working on an android app that writes and reads .PNG files from sdcard.
I'm currently working on an Android app that automatically changes the phones state based
I'm currently working on an app for the Android OS that displays some data.
I am working on an Android app. It has corresponding spec/test application. As part
I working on an android app that has already been built, and i am
I am working on an Android app that utilizes the Google Maps API MapView,

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.