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

  • Home
  • SEARCH
  • 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 6471933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:16:58+00:00 2026-05-25T06:16:58+00:00

I’m just trying to parse a simple JSON string in the following format: [X,Y,Z]

  • 0

I’m just trying to parse a simple JSON string in the following format: [“X”,”Y”,”Z”] and put it into a listview. The listview is within a view flipper in order to have tabs visible at all times throughout the app.

The problem i’m having is the app is stopping unexpectedly, here is the full code for the main launching activity:

public class Activity extends TabActivity implements OnClickListener {

Button doSomething;
TabHost tabHost;
ViewFlipper flipper;
ListView listview;
HttpResponse re;
String json;
JSONObject j;



@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);



    setContentView(R.layout.tablayout_1);




    doSomething = (Button) findViewById(R.id.btn_do_something);
    doSomething.setOnClickListener(this);

    flipper = (ViewFlipper) findViewById(R.id.layout_tab_one);

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







   //@SuppressWarnings("unchecked")
   ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetch());



   listview.setAdapter(adapter);

    flipper.setOnClickListener(this);

    String tabname1 = getString(R.string.tabexample_tab1);
    String tabname2 = getString(R.string.tabexample_tab2);
    String tabname3 = getString(R.string.tabexample_tab3);
    String tabname4 = getString(R.string.tabexample_tab4);

    tabHost = getTabHost();
    tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.layout_tab_one).setIndicator(tabname1));
    tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.layout_tab_two).setIndicator(tabname2));
    tabHost.addTab(tabHost.newTabSpec("tab3").setContent(R.id.layout_tab_three).setIndicator(tabname3));
    tabHost.addTab(tabHost.newTabSpec("tab4").setContent(R.id.layout_tab_four).setIndicator(tabname4));

    tabHost.setCurrentTab(0);




    listview.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
             flipper.showNext();


        }});




}



public ArrayList<String> fetch()
{
    ArrayList<String> listItems = new ArrayList<String>();

    try {
        URL twitter = new URL(
                "JSON.php");
        URLConnection tc = twitter.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));


        String line; 


        while ((line = in.readLine()) != null) {




      //make sure youe String line is completely filled after that..
      if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
       {
      JSONArray jArray = new JSONArray(line);
      for (int i = 0; i < jArray.length(); i++) 
       {
          JSONObject jobj = jArray.getJSONObject(i);
         // also make sure you get the value from the jsonObject using some key
         // like, jobj.getString("country");

         listItems.add(jobj.getString("")); 
       }
          }

        } 

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return listItems;
}





@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}


}

Log output:

    08-25 19:29:48.544: DEBUG/AndroidRuntime(850): Shutting down VM
08-25 19:29:48.544: WARN/dalvikvm(850): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): FATAL EXCEPTION: main
08-25 19:29:48.564: ERROR/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.android/com.app.android.Activity}: android.os.NetworkOnMainThreadException
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.os.Looper.loop(Looper.java:132)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.ActivityThread.main(ActivityThread.java:4123)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at java.lang.reflect.Method.invoke(Method.java:491)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at dalvik.system.NativeStart.main(Native Method)
08-25 19:29:48.564: ERROR/AndroidRuntime(850): Caused by: android.os.NetworkOnMainThreadException
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at java.net.InetAddress.getAllByName(InetAddress.java:249)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at com.android.Activity.fetch(Activity.java:193)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at com.android.Activity.onCreate(Activity.java:144)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.Activity.performCreate(Activity.java:4397)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-25 19:29:48.564: ERROR/AndroidRuntime(850):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
    08-25 19:29:48.564: ERROR/AndroidRuntime(850):     ... 11 more

Ok the app is launching now but the listview is not being populated with the JSON Strings for some reason?

  • 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-25T06:16:58+00:00Added an answer on May 25, 2026 at 6:16 am

    Your app is throwing a NetworkOnMainThreadException, and the documentation says this about it:

    The exception that is thrown when an application attempts to perform a
    networking operation on its main thread.

    This is only thrown for applications targeting the Honeycomb SDK or
    higher. Applications targeting earlier SDK versions are allowed to do
    networking on their main event loop threads, but it’s heavily
    discouraged.

    Looking at your code, you call fetch() in your onCreate(), which runs on the UI Thread. You will need to do this in a background thread (or target an SDK version less than Honeycomb). Take a look at AsyncTask for some more info on this solution.

     ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetch());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.