title: Android volley get (ANY) user data from server after login
body: <p>I'm currently building an android app using android volley to handle network task. Right now i'm not sure how do i able to get the user data from the server after login. The only way for me to get it to work is that, i need to specify my URL username like <code>Username=abc</code>. Below is my java code.</p>
  
  <p><strong>(Main)Profile Fragment.java</strong></p>
  
  <pre><code>public class ProfileFragment extends Fragment implements View.OnClickListener{
  
  ProgressDialog dialog;
  
  public ProfileFragment() {
      // Required empty public constructor
  }
  
  public static final String JSON_URL = "http://192.168.1.6/profile.php?Username=abc";
  
  private ListView listView;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
  
      dialog = new ProgressDialog(getActivity());
      dialog.setMessage("Loading....");
      dialog.show();
  
      View rootView = inflater.inflate(R.layout.activity_listview, container, false);
  
      listView = (ListView) rootView.findViewById(R.id.listView);
  
      sendRequest();
  
      return rootView;
  
  }
  
  private void sendRequest(){
  
      StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL, new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
                      showJSON(response);
                      dialog.dismiss();
  
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
                  }
              });
  
      RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
      requestQueue.add(stringRequest);
  }
  
  private void showJSON(String json){
      Profile_Details pj = new Profile_Details(json);
      pj.parseJSON();
      profile_details cl = new profile_details(getActivity(), profile_details.names, profile_details.date_of_birth,  profile_details.street_add, profile_details.postal_code, profile_details.city, profile_details.state, profile_details.country);
      listView.setAdapter(cl);
  }
  
  @Override
  public void onClick(View v) {
      sendRequest();
  }
  
  }
  </code></pre>
  
  <p><strong>Profile.php</strong></p>
  
  <pre><code>&lt;?php
  
  define('HOST','localhost');
  define('USER','root');
  define('PASS','');
  define('DB','users');
  
  $Username = $_GET["Username"];
  
  $mysqli = mysqli_connect(HOST,USER,PASS,DB);
  
  $stmt = $mysqli-&gt;prepare ("SELECT Name, Date_of_Birth, 
  Street_Add, Postal_Code, City, State, Country
                         FROM personal_details INNER JOIN user_details ON 
  user_details.User_ID = personal_details.User_ID 
                         WHERE Username=?");
  
  $stmt-&gt;bind_param('s', $Username);
  $stmt-&gt;execute();
  /* bind variables to prepared statement */
  $stmt-&gt;bind_result($Name, $Date_of_Birth, 
  $Street_Add, $Postal_Code, $City, $State, $Country);
  $rows = array();
  /* fetch values */
  
  while($stmt-&gt;fetch()) {
          array_push($rows, array('Name'=&gt;$Name, 
                    'Date_of_Birth'=&gt;$Date_of_Birth, 
                    'Street_Add'=&gt;$Street_Add, 'Postal_Code'=&gt;$Postal_Code, 
                    'City'=&gt;$City, 'State'=&gt;$State, 
                    'Country'=&gt;$Country));
  }
  
  /* close statement */
  $stmt-&gt;close();
  
  /* close connection */
  $mysqli-&gt;close();
  echo json_encode(array("result"=&gt;$rows));
  
  ?&gt;
  </code></pre>
  
  <p>Additional info. I created the <code>personal details</code> table that is linked from <code>user details</code> table. I think the linking part is ok. Below is my json response.</p>
  
  <p><strong>JSON</strong></p>
  
  <pre><code>{
  "result": [{
      "Name": "John",
      "Date_of_Birth": "1995-05-09",
      "Street_Add": "abc street",
      "Postal_Code": "12345",
      "City": "def",
      "State": "ghi",
      "Country": "jkl"
  }]
  }
  </code></pre>
  
  <p>So how do i configure the java code that after i login, i can straight display the user profile that i do not need to specify a username at the end of <code>URL</code>.</p>
  
  <p><strong>New update!</strong>
  I got a null object reference error</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                         Process: com.androidnewlearner, PID: 2885
                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.HashMap com.androidnewlearner.content.SessionManager.getUserDetails()' on a null object reference
                                                             at com.androidnewlearner.mainactivity.ProfileFragment.sendRequest(ProfileFragment.java:84)
                                                             at com.androidnewlearner.mainactivity.ProfileFragment.onCreateView(ProfileFragment.java:74)
                                                             at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                             at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                             at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                             at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                             at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                             at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                             at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                             at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                             at android.os.Looper.loop(Looper.java:148)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>
  
  <p>Here is my <code>SessionManager</code> java file. I copy only the line that error pointed to.</p>
  
  <p><strong>SessionManager.java</strong></p>
  
  <pre><code>public HashMap&lt;String, String&gt; getUserDetails(){
      // Use hashmap to store user credentials
      HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;();
  
      // User name
      user.put(KEY_USERNAME, pref.getString(KEY_USERNAME, null));
  
      // User password
      user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
  
      // return user
      return user;
  }
  </code></pre>
  
  <p><strong>Updated Profile Fragment.java</strong></p>
  
  <pre><code>public class ProfileFragment extends Fragment implements View.OnClickListener{
  
  ProgressDialog dialog;
  String url;
  
  SessionManager session;
  private String Username;
  private String Acc_Pass;
  SharedPreferences shared;
  
  public ProfileFragment() {
      // Required empty public constructor
  }
  
  private ListView listView;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
  
      dialog = new ProgressDialog(getActivity());
      dialog.setMessage("Loading....");
      dialog.show();
  
      View rootView = inflater.inflate(R.layout.activity_listview, container, false);
      //buttonGet = (Button) rootView.findViewById(R.id.buttonGet);
      //buttonGet.setOnClickListener(this);
  
      listView = (ListView) rootView.findViewById(R.id.listView);
  
      sendRequest(Username, Acc_Pass);  &lt;------Here is the error
      shared= getActivity().getSharedPreferences("Mypref", Context.MODE_PRIVATE);
  
      return rootView;
  
      //return inflater.inflate(R.layout.fragment_profile, container, false);
  }
  
  private void sendRequest(String Username, String Acc_Pass){
  
      HashMap&lt;String, String&gt; user = session.getUserDetails();  &lt;------Here is the error
      Username = user.get(SessionManager.KEY_USERNAME);
      Acc_Pass = user.get(SessionManager.KEY_PASSWORD);
  
      RequestQueue requestQueue = VolleyController.getInstance(getActivity().getApplicationContext()).getRequestQueue();
  
      String url = "http://192.168.1.6/json_dri_details2.php?Username="+Username+"&amp;Acc_Pass="+Acc_Pass+"";
  
      url = url.replaceAll(" ", "%20");
      try {
          URL sourceUrl = new URL(url);
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }
      Log.i("Getting url info",""+url);
  
      StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
                      showJSON(response);
                      dialog.dismiss();
  
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
                      dialog.dismiss();
                  }
              });
  
      requestQueue.add(stringRequest);
  }
  
  private void showJSON(String json){
      Dri_Details pj = new Dri_Details(json);
      pj.parseJSON();
      profile_details cl = new profile_details(getActivity(), profile_details.names, profile_details.date_of_birth,  profile_details.street_add, profile_details.postal_code, profile_details.city, profile_details.state, profile_details.country);
      listView.setAdapter(cl);
  }
  
  @Override
  public void onClick(View v) {
      sendRequest(Username, Acc_Pass);
  }
  
  }
  </code></pre>