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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:46:36+00:00 2026-06-13T10:46:36+00:00

Hello i have this code and it gives me errors on android 3 and

  • 0

Hello i have this code and it gives me errors on android 3 and 4 devices.
I know that i can solve this with async but i haven’t worked with sync task before and don’t have a clue how this works. can someone give me a little example with my code or a suggestion how to implement it?

my code:

package com.wemait.obd.emmen;

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

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

import com.wemait.obd.emmen.JSONParser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Nieuws extends Activity {


// url om request te maken
private static String URL = "http://www.iappministrator.com/OBDE/webservice/android_news_items.php";

// JSON Node namen
static final String TAG_ITEMS = "items";
static final String TAG_TITLE = "title";
static final String TAG_MESSAGE = "message";
static final String TAG_DATE = "date";
static final String TAG_IMAGES = "images";

// Nieuws JSONArray
JSONArray newsArray = null;

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nieuws);

    // Hashmap voor listView
    final ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

    // Maak een JSON Parser instance
    JSONParser jParser = new JSONParser();

    // Pakt JSON string uit URL
    JSONObject json = jParser.getJSONFromUrl(URL);

    try{
        // Pakt de Array van Nieuwsartikelen
        newsArray = json.getJSONArray(TAG_ITEMS);

        // Loop door alle Nieuwsartikels
        for(int i=0; i < newsArray.length(); i++){
            JSONObject c = newsArray.getJSONObject(i);

            // Het plaatsen van elk json item in variabele
            String title = c.getString(TAG_TITLE);
            String message = c.getString(TAG_MESSAGE);
            //String date = c.getString(TAG_DATE);
            String images = c.getString(TAG_IMAGES);

            // maak een nieuwe HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // voeg elk item child node in de Hashmap -> value
            map.put(TAG_TITLE, title);
            map.put(TAG_MESSAGE, message);
            //map.put(TAG_DATE, date);
            map.put(TAG_IMAGES, images);


            // voeg de HashList toe aan ArrayList
            newsList.add(map);
        }
    } catch(JSONException e){
        e.printStackTrace();
    }


    list=(ListView)findViewById(R.id.list);

    adapter=new LazyAdapter(this, newsList, R.layout.list_row, 
            new String[]{TAG_TITLE, TAG_MESSAGE, TAG_DATE, TAG_IMAGES}, new int[] {
            R.id.title, R.id.artist, R.id.duration, R.id.list_image});        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              HashMap<String, String> map = newsList.get(position);

             Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            //Intent in = new Intent(SingleMenuItemActivity.this, org.scout.android.library.LibraryDetail.class);
            in.putExtra(TAG_TITLE, map.get(TAG_TITLE));
            in.putExtra(TAG_MESSAGE, map.get(TAG_MESSAGE));                         
            in.putExtra(TAG_DATE, map.get(TAG_DATE));
            in.putExtra(TAG_IMAGES, map.get(TAG_IMAGES));

            startActivity(in);
        /* old code public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String date = ((TextView) view.findViewById(R.id.duration)).getText().toString();
            String message = ((TextView) view.findViewById(R.id.artist)).getText().toString();
            //String images = ((TextView) view.findViewById(R.id.list_image)).getText().toString();
            //ImageView thumb_image = (ImageView) view.findViewById(R.id.thumbnail); // thumb image
            String images = ((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();
            //String images = ((ImageView)view.findViewById(R.id.list_image)).toString();
            //String images = ((ImageView)view.findViewById(R.id.list_image)).getContext().toString();

            // Start de nieuwe intent

            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            //Bitmap bitmap = (Bitmap) in.getParcelableExtra(TAG_IMAGES);

            in.putExtra(TAG_TITLE, title);
            in.putExtra(TAG_DATE, date);
            in.putExtra(TAG_MESSAGE, message);
            in.putExtra(TAG_IMAGES, images);
            startActivity(in);*/

        }
    });     
}   

}

The error i received:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wemait.obd.emmen/com.wemait.obd.emmen.Nieuws}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.wemait.obd.emmen.JSONParser.getJSONFromUrl(JSONParser.java:38)
at com.wemait.obd.emmen.Nieuws.onCreate(Nieuws.java:53)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

My adapter class:

    package com.wemait.obd.emmen;

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

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, int listRow, String[] strings, int[] is) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.TAG_TITLE));
        artist.setText(song.get(CustomizedListView.TAG_MESSAGE));
        duration.setText(song.get(CustomizedListView.TAG_DATE));
        imageLoader.DisplayImage(song.get(CustomizedListView.TAG_IMAGES), thumb_image);
        return vi;
    }
}
  • 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-13T10:46:37+00:00Added an answer on June 13, 2026 at 10:46 am

    Android 3 and 4 have this restriction. You can not run any network related task on main thread. You either have to create a separate thread for this or use async task. I will recommend AsynTask but if you want to solve this by threads then use thread and handler to achieve it. That is how you code will look like if you’ll use AsynTask 🙂

    public class Nieuws extends Activity{
    private static String URL = "http://www.iappministrator.com/OBDE/webservice/android_news_items.php";
    
    // JSON Node namen
    static final String TAG_ITEMS = "items";
    static final String TAG_TITLE = "title";
    static final String TAG_MESSAGE = "message";
    static final String TAG_DATE = "date";
    static final String TAG_IMAGES = "images";
    
    // Nieuws JSONArray
    JSONArray newsArray = null;
    
    ListView list;
    LazyAdapter adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nieuws);
        list=(ListView)findViewById(R.id.list);
    
        new GetJSONData().execute();
    
    }
    
    class GetJSONData extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>{
    
    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
        // Hashmap voor listView
        final ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
    
        // Maak een JSON Parser instance
        JSONParser jParser = new JSONParser();
    
        // Pakt JSON string uit URL
        JSONObject json = jParser.getJSONFromUrl(URL);
    
        try{
            // Pakt de Array van Nieuwsartikelen
            newsArray = json.getJSONArray(TAG_ITEMS);
    
            // Loop door alle Nieuwsartikels
            for(int i=0; i < newsArray.length(); i++){
                JSONObject c = newsArray.getJSONObject(i);
    
                // Het plaatsen van elk json item in variabele
                String title = c.getString(TAG_TITLE);
                String message = c.getString(TAG_MESSAGE);
                //String date = c.getString(TAG_DATE);
                String images = c.getString(TAG_IMAGES);
    
                // maak een nieuwe HashMap
                HashMap<String, String> map = new HashMap<String, String>();
    
                // voeg elk item child node in de Hashmap -> value
                map.put(TAG_TITLE, title);
                map.put(TAG_MESSAGE, message);
                //map.put(TAG_DATE, date);
                map.put(TAG_IMAGES, images);
    
    
                // voeg de HashList toe aan ArrayList
                newsList.add(map);
            }
        } catch(JSONException e){
            e.printStackTrace();
        }
        return newsList;
    }
    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    
    
    
        adapter=new LazyAdapter(this, result, R.layout.list_row, 
                new String[]{TAG_TITLE, TAG_MESSAGE, TAG_DATE, TAG_IMAGES}, new int[] {
                R.id.title, R.id.artist, R.id.duration, R.id.list_image});        
        list.setAdapter(adapter);
    
    
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                  HashMap<String, String> map = newsList.get(position);
    
                 Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                //Intent in = new Intent(SingleMenuItemActivity.this, org.scout.android.library.LibraryDetail.class);
                in.putExtra(TAG_TITLE, map.get(TAG_TITLE));
                in.putExtra(TAG_MESSAGE, map.get(TAG_MESSAGE));                         
                in.putExtra(TAG_DATE, map.get(TAG_DATE));
                in.putExtra(TAG_IMAGES, map.get(TAG_IMAGES));
    
                startActivity(in);
    
            }
        });
    }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have this code that works perfect in Firefox but in IE 8
hello I have this code that use to sort a datatable. Dim sortingIndex As
Hello I have this code class Triplets { public: int nVal1; int nVal2; NodeT
Hello I currently have this code checking if a cookie exists, it works all
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Lets say I have this code: <?php class hello { var $greeting = hello;
Problem Hello all! I have this code which takes my jpg image loops through
Hello i have this form filling javascript: function onLine(code,nn) { document.writeform.bericht.value+=code; document.writeform.bericht.focus(); document.writeform.nickname.value+=nn; write1();
I have run this Perl code: #!/usr/bin/perl print content-type: text/html \n\n; print Hello World.\n;
Hello you naughty code animals.. I have tried to get this code to work,

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.