title: how to get the JSON DATA from a page loaded in fragment and datacalled via asyncbackground
body: <p>Hi im trying to display the data I fetch from a webservice. I manage the get the data via async background. im trying to display it view listview I loaded via fragment. kindly see the code below.</p>
  
  <pre><code>this is my MainActivity.java
  
      public class MainActivity extends Activity
      {
          private DrawerLayout mDrawerLayout;
          private ListView mDrawerList;
          private ActionBarDrawerToggle mDrawerToggle;
  
          private CharSequence mDrawerTitle;
          private CharSequence mTitle;
          private String[] mActions;
  
          public JSONArray blogData;
          public String[] blogPostTitle;
          private int position;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              ListView listView;
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              mTitle = mDrawerTitle = getTitle();
              mActions = getResources().getStringArray(R.array.action_array);
              mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
              mDrawerList = (ListView) findViewById(R.id.left_drawer);
  
              // set a custom shadow that overlays the main content when the drawer opens
              //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
              // set up the drawer's list view with items and click listener
              mDrawerList.setAdapter(new ArrayAdapter&lt;String&gt;(this,
                      R.layout.drawer_list_item, mActions));
              mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
  
              // enable ActionBar app icon to behave as action to toggle nav drawer
              getActionBar().setDisplayHomeAsUpEnabled(true);
              getActionBar().setHomeButtonEnabled(true);
  
              // ActionBarDrawerToggle ties together the the proper interactions
              // between the sliding drawer and the action bar app icon
              mDrawerToggle = new ActionBarDrawerToggle(
                      this,                  /* host Activity */
                      mDrawerLayout,         /* DrawerLayout object */
                      R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                      R.string.drawer_open,  /* "open drawer" description for accessibility */
                      R.string.drawer_close  /* "close drawer" description for accessibility */
              ) {
                  public void onDrawerClosed(View view) {
                      getActionBar().setTitle(mTitle);
                      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                  }
  
                  public void onDrawerOpened(View drawerView) {
                      getActionBar().setTitle(mDrawerTitle);
                      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                  }
              };
              mDrawerLayout.setDrawerListener(mDrawerToggle);
  
              if (savedInstanceState == null) {
                  selectItem(0);
              }
  
          }
  
          //public void onItemClick(AdapterView&lt;?&gt; adapter, View view,
          //                        int position, long id) {
          //    Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          //            Toast.LENGTH_SHORT).show();
          //}
  
          public void signInClick(View v) {
              //Button button = (Button) v;
              //startActivity(new Intent(getApplicationContext(), PatientActivity.class));
          }
  
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
              MenuInflater inflater = getMenuInflater();
              inflater.inflate(R.menu.main, menu);
              return super.onCreateOptionsMenu(menu);
          }
  
          /* Called whenever we call invalidateOptionsMenu() */
          @Override
          public boolean onPrepareOptionsMenu(Menu menu) {
              // If the nav drawer is open, hide action items related to the content view
              //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
              return super.onPrepareOptionsMenu(menu);
          }
  
  
          /* */
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              // The action bar home/up action should open or close the drawer.
              // ActionBarDrawerToggle will take care of this.
              if (mDrawerToggle.onOptionsItemSelected(item)) {
                  return true;
              }
                      return super.onOptionsItemSelected(item);
          }
  
  
          /* The click listener for ListView in the navigation drawer */
          private class DrawerItemClickListener implements ListView.OnItemClickListener {
              int pos;
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  selectItem(position);
  
              }
          }
  
          private void selectItem(int position) {
              // update the main content by replacing fragments
              Fragment fragment = new ActionFragment();
              Bundle args = new Bundle();
              args.putInt(ActionFragment.ARG_ACTION1_NUMBER, position);
              fragment.setArguments(args);
  
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              setTitle(mActions[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
  
          }
  
          @Override
          public void setTitle(CharSequence title) {
              mTitle = title;
              getActionBar().setTitle(mTitle);
          }
  
          /**
           * When using the ActionBarDrawerToggle, you must call it during
           * onPostCreate() and onConfigurationChanged()...
           */
  
          @Override
          protected void onPostCreate(Bundle savedInstanceState) {
              super.onPostCreate(savedInstanceState);
              // Sync the toggle state after onRestoreInstanceState has occurred.
              mDrawerToggle.syncState();
          }
  
          @Override
          public void onConfigurationChanged(Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
              // Pass any configuration change to the drawer toggles
              mDrawerToggle.onConfigurationChanged(newConfig);
          }
      }
  </code></pre>
  
  <p>UPDATE 1.1</p>
  
  <p>this is my ActionFragment data that calls loads the listview. I use this because I have a navigation drawer which load the listview once an option to the menu is selected.</p>
  
  <pre><code>public class ActionFragment extends Fragment
  {
      public static final String ARG_ACTION1_NUMBER = "Fragment_layout";
  
      public JSONArray blogData;
      public String[] blogPostTitle;
  
      public ActionFragment() {
          // Empty constructor required for fragment subclasses
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
          int i = getArguments().getInt(ARG_ACTION1_NUMBER);
  
          if (i == 0)
          {
              rootView = inflater.inflate(R.layout.fragment_layout, container, false);
          }
          else if (i == 1) {
              rootView = inflater.inflate(R.layout.fragment_articles, container, false);
              ListView listview1 = (ListView) rootView.findViewById(R.id.article_list);
  
              ArrayAdapter&lt;String&gt; arrayAdapter1 = new ArrayAdapter&lt;String&gt;(getActivity(), R.layout.layout_listitem, R.id.titleTV, blogPostTitle);
              listview1.setAdapter(arrayAdapter1);
  
          } else if (i == 2)
          {
              rootView = inflater.inflate(R.layout.fragment_news, container, false);
          } else if (i == 3) {
              rootView = inflater.inflate(R.layout.fragment_signin, container, false);
          }
          return rootView;
      }
  
      public void articleList()
      {
          if (blogData == null)
          {
              // TODO: update list when null.
          }
          else
          {
              try
              {
                  blogPostTitle = new String[blogData.length()];
                  for (int i = 0; i &lt; blogData.length(); i++)
                  {
                      JSONObject jsonObject = blogData.getJSONObject(i);
                      String title = jsonObject.getString("title");
                      title = Html.fromHtml(title).toString();
                      blogPostTitle[i] = title;
                  }
  
  
              } catch (JSONException e) {
                  Log.e("TAG", "Exception Caught: ", e);
              }
          }
      }
  
      private final MyListener mListener = new MyListener()
      {
  
          @Override
          public void onComplete(JSONArray result) {
  
              PostController postActivity = new PostController(mListener);
              postActivity.execute();
              blogData = result;
              articleList();
          }
      };
  
  }
  </code></pre>
  
  <p>and this is my PostController.java which connect to the webservice</p>
  
  <pre><code>public class PostController extends AsyncTask&lt;Object, Void, JSONArray&gt; {
  
      MyListener mListener;
  
      public PostController(MyListener listenr) {
          mListener = listenr;
      }
  
      @Override
      protected JSONArray doInBackground(Object... arg0)
      {
  
          String result = "No data available";
          JSONArray jsonResponse = null;
  
          try
          {
              DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
              //TODO: URL ADDRESS for Articles.
              HttpGet httpGet = new HttpGet("http://localhost/public/webservices/post");
              httpGet.setHeader("Content-type", "application/json");
  
              InputStream inputStream = null;
  
              HttpResponse response = httpclient.execute(httpGet);
              HttpEntity entity = response.getEntity();
  
              inputStream = entity.getContent();
              // json is UTF-8 by default
              BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
              StringBuilder sb = new StringBuilder();
  
              String line = null;
              while ((line = reader.readLine()) != null)
              {
                  sb.append(line);
              }
              result = sb.toString();
              jsonResponse = new JSONArray(result);
  
              // TODO: Remove log.
              Log.d("TAG", String.valueOf(jsonResponse));
          }
          catch (MalformedURLException e)
          {
              Log.e("TAG", "exception caught: ", e);
          }
          catch (IOException e)
          {
              Log.e("TAG", "exception caught: ", e);
          }
          catch (Exception e)
          {
              Log.e("TAG", "exception caught: ", e);
          }
          return jsonResponse;
      }
  
      @Override
      protected void onPostExecute(JSONArray result)
      {
          mListener.onComplete(result);
  
      }
  }
  </code></pre>
  
  <p>Here is the log:</p>
  
  <pre><code>05-08 08:07:27.189    2585-2585/medapp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: medapp.app, PID: 2585
  java.lang.NullPointerException: storage == null
          at java.util.Arrays$ArrayList.&lt;init&gt;(Arrays.java:38)
          at java.util.Arrays.asList(Arrays.java:155)
          at android.widget.ArrayAdapter.&lt;init&gt;(ArrayAdapter.java:141)
          at medapp.app.ActionFragment.onCreateView(ActionFragment.java:42)
          at android.app.Fragment.performCreateView(Fragment.java:1700)
          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
          at android.app.BackStackRecord.run(BackStackRecord.java:684)
          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
          at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
          at android.os.Handler.handleCallback(Handler.java:733)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:136)
          at android.app.ActivityThread.main(ActivityThread.java:5017)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>UPDATE 3</p>
  
  <pre><code>`public class HomeFragment extends Fragment {
  
      protected ProgressBar mProgressBar;
      protected JSONArray blogPostData;
      protected String[] blogPostTitle;
  
      ListView listView;
  
      View rootView;
  
      public HomeFragment(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  rootView.findViewById(R.id.article_list);
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(), R.layout.layout_listview_article,R.id.titleTV,blogPostTitle);
          listView.setAdapter(adapter);*/
  
          rootView = inflater.inflate(R.layout.fragment_home, container, false);
          listView = (ListView) rootView.findViewById(R.id.article_list);
  
          ArticleController articleController = new ArticleController(mListener);
          articleController.execute();
          return rootView;
      }
  
      MyListener mListener = new MyListener(){
  
          @Override
          public void onComplete(JSONArray result) {
  
              // TODO: Here you will get the Jsonarray from Asynctask.
              // Process it and get the values.
              blogPostData = result;
              updateArticleList();
              ArrayAdapter&lt;String&gt; arrayAdapter1 = new ArrayAdapter&lt;String&gt;(getActivity(), R.layout.layout_listview_article, R.id.titleTV, blogPostTitle);
              listView.setAdapter(arrayAdapter1);
          }
      };
  
      protected void updateArticleList(){
  
          try {
  
              blogPostTitle = new String[blogPostData.length()];
              String[] blogPostDate = new String[blogPostData.length()];
              String[] blogPostAuthor = new String[blogPostData.length()];
              for (int i = 0; i &lt; blogPostData.length(); i++)
              {
                  JSONObject jsonObject = blogPostData.getJSONObject(i);
                  String title = jsonObject.getString("title");
                  title = Html.fromHtml(title).toString();
                  blogPostTitle[i] = title;
  
                  String date = jsonObject.getString("date");
                  title = Html.fromHtml(title).toString();
                  blogPostTitle[i] = title;
  
                  String author = jsonObject.getString("author");
                  title = Html.fromHtml(title).toString();
                  blogPostTitle[i] = title;
  
                  Log.d("TAG", "Title: " + blogPostTitle);
              }
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }`
  </code></pre>