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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:01:54+00:00 2026-06-14T04:01:54+00:00

In below code i download json from internet and want to show in list.

  • 0

In below code i download json from internet and want to show in list.
if list is empty go to another activity but other activity not start.
no error but no start activity.
thanks for your help

package ir.mohammadi.android.nightly;

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

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

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class AllNotes extends ListActivity {

    ProgressDialog pDialog;

    ArrayList<HashMap<String, String>> noteList;

    JSONArray notes = null;

    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR_MSG = "error_message";
    private static String KEY_NOTE_ID = "note_id";
    private static String KEY_NOTE_SUBJECT = "note_subject";
    private static String KEY_NOTE_DATE = "note_date";

    private static String EXECUTE_RESULT = null;

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

        noteList = new ArrayList<HashMap<String, String>>();
        new LoadAllNotes().execute();
        ListView lv = getListView();
    }

    public class LoadAllNotes extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AllNotes.this);
            pDialog.setMessage("لطفا صبر کنید...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            UserFunctions userFunctions = new UserFunctions();

            JSONObject jSon = userFunctions.getAllNotes("1");

            Log.i("AllNotes >> jSon >>", jSon.toString());
            try {
                String success = jSon.getString(KEY_SUCCESS);
                if (success == "1") {
                    notes = jSon.getJSONArray("notes");

                    for (int i = 0; i < notes.length(); i++) {
                        JSONObject c = notes.getJSONObject(i);

                        String id = c.getString(KEY_NOTE_ID);
                        String subject = c.getString(KEY_NOTE_SUBJECT);
                        String date = c.getString(KEY_NOTE_DATE);

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(KEY_NOTE_ID, id);
                        map.put(KEY_NOTE_SUBJECT, subject);
                        map.put(KEY_NOTE_DATE, date);

                        noteList.add(map);
                    }

                } else {

                    finish();
                    Toast.makeText(getApplicationContext(),
                            jSon.getString(KEY_ERROR_MSG), Toast.LENGTH_SHORT).show();

                    Log.i("AllNotes >> No nightly >>", "...");

                    Intent i = new Intent(getApplicationContext(), login.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {

                public void run() {
                    ListAdapter adapter = new SimpleAdapter(AllNotes.this,
                            noteList, R.layout.list_item, new String[] {
                                    KEY_NOTE_ID, KEY_NOTE_SUBJECT,
                                    KEY_NOTE_DATE }, new int[] {
                                    R.id.list_lbl_id, R.id.list_lbl_subject,
                                    R.id.list_lbl_date });
                    setListAdapter(adapter);
                }
            });
        }
    }
}

Logcat json :

11-08 22:17:44.467: I/getAllNotes params before getting from net >>(599): [tag=getNotesList, user_id=1]
11-08 22:17:47.647: I/Input stream >>(599): org.apache.http.conn.EofSensorInputStream@44ededd8
11-08 22:17:47.767: I/JSON string builder >>(599): a{"error":"1","error_message":"\u0634\u0645\u0627 \u0647\u0646\u0648\u0632 \u0634\u0628\u0627\u0646\u0647 \u0627\u06cc \u0646\u0646\u0648\u0634\u062a\u0647 \u0627\u06cc\u062f."}
11-08 22:17:47.797: I/getAllNotes params after getting from net >>(599): {"error":"1","error_message":"dont have any note"}
11-08 22:17:47.797: I/AllNotes >> jSon >>(599): {"error":"1","error_message":"dont have any note"}
  • 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-14T04:01:55+00:00Added an answer on June 14, 2026 at 4:01 am

    The most likely cause of your problem is this line:

    String success = jSon.getString(KEY_SUCCESS);
    

    You didn’t post it, but I’ll bet there is a JSONException stack trace below that last line. getString() will throw an exception if the key is not found, and in the JSON data you logged out, there is no “success” key. That exception is causing all the code you wrote after the fact to not get called at all, which is why you see nothing happening.

    A couple other things to note:

    1. success == "1" is not the proper way to do String equality. The == operator checks object equality (same object), if you want to check if two Strings are the same, us success.equals("1") instead.
    2. onPostExecute() is always called on the main thread for you. You do not have to call runOnUiThread() from the method…it is redundant.
    3. It is technically okay to start an Activity from a background thread like you have done, but you cannot show a Toast this way. When you fix your exception, the Toast.makeText() line will end up failing because you cannot show a Toast from a thread other than the main thread.

    In general, it’s best to do all UI operations on the main thread and as a point of design you should move any code that manipulates or changes the screen into onPostExecute() so it can be called at the right time. That’s what it is there for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below is the code to download a txt file from internet approx 9000 lines
Below code not work, but it's work fine for jsf1.2. Now the framework is
I am using the code below to download multiple attachments from a TFS server:
I want to download the code from http://pastehtml.com/view/awono3xoq.txt , save it to a string,
We currently use the code below to download files from a network server to
I am using the code below to download an Excel file. Once the Response.End()
below code is my databasehandler class i got it from a tutorial. Beside that
In below code when I click 'Vote' a vote results screen is displayed but
Using the code below each image download) file_get_contents() ) takes on average 8-15 seconds.....
Can I use the PHP code below if the PDF file is not on

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.