2017-03-06 51 views
-1

我無法在導航抽屜中顯示特定用戶名。登錄成功時顯示特定配置文件?

http://192.168.100.5:84/api/academics/students是學生總數的api。

當我與下面給出的具體ID一起使用時,名稱被顯示,但是它是靜態的,提供了id.i想要發送id作爲具有特定id的路線 http://192.168.100.5:84/api/academics/students/1(id)中的參數。 這是如何實現的?

{ 

    "StdID":1, 
    "NAME":"Kirsten Green", 
    "PHONENO":"095-517-0049", 
    "DOB":"2009-12-28T00:00:00", 
    "CLASS":9, 
    "GENDER":"M", 
    "ADDRESS":"8254 At Ave", 
    "NATIONALITY":"Belgium", 
    "ENROLLEDYEAR":"2016-04-21T00:00:00", 
    "Photo":null, 
    "Cat_ID":5, 
    "base64":null, 
    "studentDetails":{ 
     "StdID":1, 
     "GUARDIAN_PHONE_NO":"002-283-4824", 
     "MOBILE_NO":"1-377-762-8548", 
     "First_NAME":"Maile", 
     "Last_Name":"Lancaster", 
     "Relation":"Father", 
     "DOB":"2017-02-22T00:00:00", 
     "Education":"Ph.D", 
     "Occupation":"Etiam ligula tortor,", 
     "Income":"20000-30000", 
     "Email":"[email protected]", 
     "AddLine1":"Ap #416-4247 Sollicitudin Av.", 
     "AddLine2":"Ap #801-7380 Imperdiet Avenue", 
     "State":"ME", 
     "Country":"Israel" 
    }, 
    "Marks":null, 
    "stdCategory":{ 
     "Cat_ID":5, 
     "Category":"Normal" 
    } 

} 

米亞瓦類

公共類家庭擴展AppCompatActivity實現View.OnClickListener {

LinearLayout calendar, classSchedule, progressReport, profile, fee, dshboard, setting, logout, attendance; 
android.support.v4.app.FragmentManager fragmentManager; 
public static final String Navigation_URL = "http://192.168.100.5:84/api/academics/students/"; 
ImageView studentprofileimage; 
TextView profilename; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dashboard); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    studentprofileimage = (ImageView) findViewById(R.id.avatar);//initilise student name 
    profilename = (TextView) findViewById(R.id.profilename);// student profile name 


    dshboard = (LinearLayout) findViewById(R.id.dashboard_layout); 
    calendar = (LinearLayout) findViewById(R.id.calender_layout); 
    fee = (LinearLayout) findViewById(R.id.view_fee); 
    classSchedule = (LinearLayout) findViewById(R.id.class_schedule); 
    progressReport = (LinearLayout) findViewById(R.id.progress_report); 
    profile = (LinearLayout) findViewById(R.id.view_profile); 
    setting = (LinearLayout) findViewById(R.id.mainsetting); 
    logout = (LinearLayout) findViewById(R.id.mainlogout); 
    attendance = (LinearLayout) findViewById(R.id.class_attendance); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setVisibility(View.VISIBLE); 

    calendar.setOnClickListener(this); 
    classSchedule.setOnClickListener(this); 
    fee.setOnClickListener(this); 
    dshboard.setOnClickListener(this); 
    progressReport.setOnClickListener(this); 
    profile.setOnClickListener(this); 
    setting.setOnClickListener(this); 
    logout.setOnClickListener(this); 
    attendance.setOnClickListener(this); 
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
    tx.replace(R.id.container, new Dashboard()); 
    tx.commit(); 
    makeJsonObjectRequest(1);// what should be parameter 

} 


private void makeJsonObjectRequest(int stud_id) { 
    String URL = Navigation_URL + stud_id; 
    Log.d("TAG", "URL:" + URL); 
    JsonArrayRequest movieReq = new JsonArrayRequest(URL, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 


        for (int i = 0; i < response.length(); i++) { 
         try { 

          JSONObject obj = response.getJSONObject(i); 
          StudentInformation studentInformation = new StudentInformation(); 

          // Login login = new Login(); 
          //login.masterid= studentInformation.studentId = obj.getInt("StdID"); 
          // String x = login.masterid; 
          // login.masterid = String.valueOf(studentInformation.studentId = obj.getInt("StdID")); 
          // Log.d("All type", String.valueOf(x)); 

          Integer id = studentInformation.studentId = obj.getInt("StdID"); 
          String s = studentInformation.Name = obj.getString("NAME"); 
          System.out.println(); 
          profilename.setText(s); 

          Log.d("All type", String.valueOf(studentInformation.studentId)); 
          Log.d("All type", studentInformation.Name); 


         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 


       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show(); 
       } 
      }); 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(movieReq); 


} 


@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 


@Override 
public void onClick(View v) { 

    int id = v.getId(); 


    if (id == R.id.dashboard_layout) { 
     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new Dashboard()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

    } else if (id == R.id.calender_layout) { 
     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new CalenderFragment()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.view_fee) { 
     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new Fee()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

    } else if (id == R.id.class_schedule) { 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new FragmentClassSchedule()) 
       .commit(); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.progress_report) { 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new ProgressFragment()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.class_attendance) { 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new AttendanceStudentFragment()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.view_profile) { 


     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new ProfileFragment()) 
       .commit(); 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.mainsetting) { 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new SettingFragment()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else if (id == R.id.mainlogout) { 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new Dashboard()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.container, new Dashboard()) 
       .commit(); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

    } 


} 

}

如何具體的ID來發送,以便將顯示具體名稱?

回答

1
public static final String Navigation_URL = "http://192.168.100.5:84/api/academics/students/"; 

    private void makeJsonObjectRequest(int stud_id) { 
    String URL=Navigation_URL +stud_id; 
    Log.d("TAG","URL:"+URL); 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonObject = new JSONObject(response); 
         StudentInformation studentInformation = new StudentInformation(); 
         studentInformation.Name = jsonObject.getString("NAME"); 
         profilename.setText(studentInformation.Name); 
        } catch (JSONException e) { 
         Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show(); 
         e.printStackTrace(); 
        } 

       } 

      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show(); 
       } 
      }); 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 


} 
+0

@ user2025187I得到意外的響應代碼500錯誤。 – seon

+0

您將您的Navigation_URL更改爲「http://192.168.100.5:84/api/academics/students/」? – user2025187

+0

更改顯示,請看。@ user2025187 – seon

0

你傳入一個字符串StdID,而不是你需要傳遞一個值。

改變你的方法makejsonobject接受一些字符串值並傳遞它。