Nitesh Singh
Nitesh Singh

Reputation: 809

Trying to parse json to list view on android but getting error

I am making a app which fetches json from my websites and parses that json to listview on android.

I get the json using http request then make 2 arrays websites t hold all websites names and links to hold links.I want the listview to show websites names and on clicking then open the website in the browser.

Can anyone please help me.Tried everything I could. Help me figure out the problem or tell me another way to do this thanks.

Trying To get the json and parse it into a listView Below is my code:

public class GetWebsiteList extends AsyncTask<String, String, String> {
        // Creating JSON Parser object

        ArrayList<HashMap<String, String>> productsList;
        String websites[]=new String[10];
        String links[]=new String[10];
        // url to get all products list
        private String url_all_products = "http://androidtest.cu.cc/getwebsites.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_WEBSITES = "websites";
        private static final String TAG_SNO = "sno";
        private static final String TAG_NAME = "name";
        private static final String TAG_LINK = "link";
        // products JSONArray
        JSONArray products = null;
        // Progress Dialog
        private ProgressDialog pDialog;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(mainpage.this);
            pDialog.setMessage("Loading Website List.Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
// Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONParser jParser = new JSONParser();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
            //ArrayAdapter adapter = ArrayAdapter.
            // Check your log cat for JSON reponse
            Log.d("nitrek All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);
                String suc;
                switch(success)
                {
                    case 1:
                        suc="True";
                        break;
                    case 0:
                        suc="False";
                        break;
                    default:
                        suc="unkonwn";
                        break;
                }

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    Log.d(" nitrek success",suc);
                    products = json.getJSONArray(TAG_WEBSITES);
                    // looping through All Products
                    Log.d(" nitrek websites", products.toString());
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);
                        // Storing each json item in variable
                        String id = c.getString(TAG_SNO);
                        String name = c.getString(TAG_NAME);
                        String link= c.getString(TAG_LINK);
                        Log.d("nitrek website",id+name+link);
                       // websites[i]=new String();
                        websites[i]=name;
                        links[i]=link;
                        Log.d("nitrek web",websites[i]+links[i]);
                        /* creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                       try {
                           map.put(TAG_SNO, id);
                           map.put(TAG_NAME, name);
                           productsList.add(map);
                       }
                       catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                        // adding HashList to ArrayList
                        */
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    Toast.makeText(mainpage.this,"no website found", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(final String result) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     */
                    //ListAdapter adapter = new SimpleAdapter(mainpage.this, productsList, R.layout.listitem, new String[]{TAG_SNO, TAG_NAME},new int[]{R.id.sno, R.id.name});
                    // updating listview
                    try {
                        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(mainpage.this, R.layout.listitem, R.id.name, websites);

                        final ArrayList<String> list = new ArrayList<String>();
                        for (int i = 0; i < websites.length; ++i) {
                            list.add(websites[i]);
                        }
                        final StableArrayAdapter adapter = new StableArrayAdapter(mainpage.this, R.layout.listitem, list);
                        //ArrayAdapter<String> ad = new ArrayAdapter<String>(mainpage.this, R.layout.listitem, websites);
                        ListView lv = (ListView) findViewById(R.id.list);
                        lv.setAdapter(adapter);
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                //Intent intent = new Intent(this, homepage.class);
                                count = 0;
                                String url = links[position];
                                Toast.makeText(mainpage.this, "Opening: " + url, Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                //notify_test(url);
                                startActivity(i);

                            }
                        });
                    } catch (Exception e) {
                        Log.d("error list nitrekerror", "below");
                        e.printStackTrace();
                    }
                    Toast.makeText(mainpage.this, result, Toast.LENGTH_LONG).show();

                    int i = 0;
                    while (i < websites.length && i < 3) {
                        Toast.makeText(mainpage.this, websites[i] + " " + links[i], Toast.LENGTH_LONG).show();
                        i++;
                    }
                }
            });
        }
    }
    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }

but I am getting this error and not able to figure out why? Below is the logcat:

07-08 00:58:34.245  27826-27826/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: nitz.nitrek.myrtoguide, PID: 27826
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
            at   android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java)
            at android.widget.AbsListView.obtainView(AbsListView.java)
            at android.widget.ListView.measureHeightOfChildren(ListView.java)
            at android.widget.ListView.onMeasure(ListView.java)
            at android.view.View.measure(View.java)
            at android.widget.RelativeLayout.measureChild(RelativeLayout.java)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java)
            at android.view.View.measure(View.java)
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java)
            at android.view.View.measure(View.java)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
            at android.view.View.measure(View.java)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
            at android.view.Choreographer.doCallbacks(Choreographer.java)
            at android.view.Choreographer.doFrame(Choreographer.java)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
            at android.os.Handler.handleCallback(Handler.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
07-08 00:58:34.245     780-3917/? E/ActivityManager﹕ App crashed! Process: nitz.nitrek.myrtoguide

Upvotes: 4

Views: 108

Answers (2)

rramakrishnaa
rramakrishnaa

Reputation: 827

You had declared the websites array of size 10 but you were filling it with only 2-3 entries depending of number of websites you got as response so on passing the whole array to arrayadapter you were getting a null pointer exception.So you got the number of websites received in response and dynamically declared the array of that size only.

Upvotes: 1

Timo
Timo

Reputation: 513

You have forgot to override getView off ArrayAdapter. And sync about viewHolder for more performance. You can read this https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

You need to override this method to inflate your view else any view is create by your adapter and throw null Exception.

Example of override :

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   Object data = yourTabObj[position] 
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_listView, parent, false);
   }
   // Lookup view for data population
   TextView title = (TextView) convertView.findViewById(R.id.title);
   title.setText(data.name);
   // Return the completed view to render on screen
   return convertView;

}

After you understand that think about viewHolder to increase performance.

Upvotes: 0

Related Questions