使用Volley通过id解析JSON数据?

时间:2017-02-23 10:57:02

标签: android json android-volley

我能够通过在url中提供个人id来解析名称(http://192.168.100.5:84/api/academics/students/1这里1是id)。实际上,当我登录系统时,它需要用户名和密码,然后Intent到Next Page.Then我设置导航抽屉必须是用户名中的动态,因为登录用户名不同,导航抽屉上的名称应该更改。

家庭课程

public class Home extends AppCompatActivity implements 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/1";
    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();

    }


    private void makeJsonObjectRequest() {

        StringRequest stringRequest = new StringRequest(Request.Method.GET, Navigation_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);


    }

我只解析了这个

{

    "StdID":1,
    "NAME":"Kirsten Green",
    "PHONENO":"095-517-0049",
    "DOB":"2009-12-28T00:00:00",
    "CLASS":9,
    "GENDER":"M",
    "ADDRESS":"8254 At Ave"}

我很困惑,家庭班级的逻辑是,当用户名改变时,抽屉上的名字应该改变。 实际上网址应为http://192.168.100.5:84/api/academics/students

{

    "StdID":14,
    "NAME":"Thaddeus Lewis",
    "PHONENO":"025-718-3841",
    "DOB":"2016-04-12T00:00:00",
    "CLASS":8,
    "GENDER":"a",
    "ADDRESS":"6024 Scelerisque Rd.",
    "NATIONALITY":"Liberia",
    "ENROLLEDYEAR":"2017-03-02T00:00:00",
    "Photo":null,
    "Cat_ID":5,
    "base64":null,
    "studentDetails":{
        "StdID":14,
        "GUARDIAN_PHONE_NO":"090-996-4913",
        "MOBILE_NO":"1-931-439-7550",
        "First_NAME":"Kitra",
        "Last_Name":"Walls",
        "Relation":"vehicula risus. Nulla eget met",
        "DOB":"2017-09-23T00:00:00",
        "Education":"malesuada augue ut lacus. Null",
        "Occupation":"urna suscipit nonummy.",
        "Income":"€135,759",
        "Email":"Donec.tincidunt.Donec@nequeSedeget.ca",
        "AddLine1":"496-5045 Ligula Rd.",
        "AddLine2":"426-283 Aenean Street",
        "State":"MAR",
        "Country":"Heard Island and Mcd"
    },
    "Marks":null,
    "stdCategory":{
        "Cat_ID":5,
        "Category":"Normal"
    }

},
{

    "StdID":15,
    "NAME":"Roanna Ramirez",
    "PHONENO":"087-467-8647",
    "DOB":"2016-04-09T00:00:00",
    "CLASS":4,
    "GENDER":"a",
    "ADDRESS":"9620 Aliquam Ave",
    "NATIONALITY":"Portugal",
    "ENROLLEDYEAR":"2017-08-03T00:00:00",
    "Photo":null,
    "Cat_ID":5,
    "base64":null,
    "studentDetails":{
        "StdID":15,
        "GUARDIAN_PHONE_NO":"002-444-8345",
        "MOBILE_NO":"1-948-383-0527",
        "First_NAME":"Mari",
        "Last_Name":"Mendez",
        "Relation":"Cras convallis",
        "DOB":"2017-08-20T00:00:00",
        "Education":"luctus lobortis. Class aptent ",
        "Occupation":"mi fringilla mi lacinia mattis",
        "Income":"€112,474",
        "Email":"amet.lorem.semper@enimnon.co.uk",
        "AddLine1":"Ap #203-6706 Gravida St.",
        "AddLine2":"Ap #416-2961 Dis Avenue",
        "State":"MI",
        "Country":"Morocco"
    },
    "Marks":null,
    "stdCategory":{
        "Cat_ID":5,
        "Category":"Normal"
    }

}

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题: 您可以在抽屉类中自行解析这些信息,您可以在其中访问配置文件名称的文本视图。 包括在抽屉类的onCreate中解析和设置textview的代码。