-1

I am not able to send access_token as header and id,accessID and currentUser as parameter using the Volley Library.

Home class

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/student?id=01&accessID=4&currentUser=01";
    ImageView studentprofileimage;
    TextView profilename, sprofilename;
    String master_id, access_token, accessID;

    @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
        sprofilename = (TextView) findViewById(R.id.student_profilename);


        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();

        SessionManagement session = new SessionManagement(Home.this);
        //  session.checkLogin();
        master_id = session.getMasterId();
        Log.d("TAG", "master_id:" + master_id);
        access_token = session.getAccesstToken();
        Log.d("TAG", "access token" + access_token);
        accessID = session.getAccess();
        Log.d("TAG", "access:" + accessID);
        makeJsonObjectRequest();


    }

    StudentInformation studentInformation;

    private void makeJsonObjectRequest() {

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String URL = Navigation_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 = new StudentInformation();
                            studentInformation.StdID = String.valueOf(jsonObject.get("StdID"));
                            studentInformation.Name = jsonObject.getString("NAME");
                            studentInformation.Gender = (String) jsonObject.get("GENDER");
                            studentInformation.Phonenumber = String.valueOf(jsonObject.get("PHONENO"));
                            studentInformation.StudentClass = String.valueOf(jsonObject.get("CLASS"));
                            studentInformation.Enrolled_Year = String.valueOf(jsonObject.get("ENROLLEDYEAR"));
                            studentInformation.Address = String.valueOf(jsonObject.get("ADDRESS"));
                            studentInformation.DOB = String.valueOf(jsonObject.get("DOB"));
                            studentInformation.Nationality = String.valueOf(jsonObject.get("NATIONALITY"));
                            profilename.setText(studentInformation.Name);
                            studentInformation.Photo = String.valueOf(jsonObject.get("Photo"));
                            studentInformation.CatID = String.valueOf(jsonObject.get("Cat_ID"));
                            studentInformation.base64 = String.valueOf(jsonObject.get("base64"));
                            studentInformation.Marks = String.valueOf(jsonObject.get("Marks"));


                            JSONObject studentDetails_obj = jsonObject.getJSONObject("studentDetails");
                            studentInformation.GuardianStudentId = studentDetails_obj.getString("StdID");
                            studentInformation.Guardianphonenumber = studentDetails_obj.getString("GUARDIAN_PHONE_NO");
                            studentInformation.Guardianmobilenumber = studentDetails_obj.getString("MOBILE_NO");
                            studentInformation.Guardianfirstname = studentDetails_obj.getString("First_NAME");
                            studentInformation.GuardianLastName = studentDetails_obj.getString("Last_Name");
                            studentInformation.GuardianRelation = studentDetails_obj.getString("Relation");
                            studentInformation.GuardianDOB = studentDetails_obj.getString("DOB");
                            studentInformation.GuardianEducation = studentDetails_obj.getString("Education");
                            studentInformation.GuardianOccupation = studentDetails_obj.getString("Occupation");
                            studentInformation.GuardianIncome = studentDetails_obj.getString("Income");
                            studentInformation.GuardianEmail = studentDetails_obj.getString("Email");
                            studentInformation.GuardianAddLine1 = studentDetails_obj.getString("AddLine1");
                            studentInformation.GuardianAddLine2 = studentDetails_obj.getString("AddLine2");
                            studentInformation.GuardianState = studentDetails_obj.getString("State");
                            studentInformation.GuardianCountry = studentDetails_obj.getString("Country");


                            JSONObject studentDetails_obj1 = jsonObject.getJSONObject("stdCategory");
                            studentInformation.Category = studentDetails_obj1.getString("Category");
                            studentInformation.CategoryId = studentDetails_obj1.getString("Cat_ID");


                        } 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();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "Bearer " + access_token);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
/*
            @Override
            protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();

                map.put("id", master_id);
                map.put("accessID", accessID);
                map.put("currentUser", master_id);
                return map;

            }    */
        };


        requestQueue.add(stringRequest);


    }


    @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) {

            Bundle bundle1 = new Bundle();
            bundle1.putSerializable("student_obj", studentInformation);

            Fragment fragment = new ProfileFragment();
            fragment.setArguments(bundle1);
            fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment);
            fragmentTransaction.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) {

            SessionManagement session = new SessionManagement(Home.this);
            session.clear();
            Intent intent = new Intent(Home.this, Login.class);
            startActivity(intent);
            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);

        }


    }


}

how can the authorization :bearer be added in the header section,which is shown in the Given picture.

enter image description here

seon
  • 1,050
  • 2
  • 13
  • 27

2 Answers2

2

how can the authorization :bearer be added in the header section,which is shown in the Given picture

Use

headers.put("Authorization", "Bearer " + access_token);

instead of

headers.put("access_token", access_token);
BNK
  • 23,994
  • 8
  • 77
  • 87
  • 404 means not found, post your full logcat info + if you use postman to test your web service, post its screenshot – BNK Mar 14 '17 at 03:24
  • logcat ] BasicNetwork.performRequest: Unexpected response code 404 for http://192.168.100.5:84/api/academics/student – seon Mar 14 '17 at 03:26
  • i have posted screenshot – seon Mar 14 '17 at 03:32
  • Comment all `getParams` content, then try using full URL in your app the same as in Postman. `getParams` or `getBody` is not for GET requests – BNK Mar 14 '17 at 03:43
  • when i use the full url it is working.How can i send student?id=01&accessID=4&currentUser=01 this as parameter dynamically. I have updated the whole code – seon Mar 14 '17 at 03:48
  • 1
    Oh, you can concat the string values, for example `URL = Navigation_URL + "?id=" + master_id + "&accessID=" + access_ID + "&currentUser=" + master_id` – BNK Mar 14 '17 at 04:00
  • Another way for your URL, you can read http://stackoverflow.com/questions/19167954/use-uri-builder-in-android-or-create-url-with-variables – BNK Mar 14 '17 at 04:03
  • i get Unexpected response code 500 http://192.168.100.5:84/api/academics/student?id=01&accessID=4&currentUser=01id=0001&accessID=4&currentUser=0001 i get double value on log.How's that possible?? – seon Mar 14 '17 at 04:05
  • I suggest you should debug to make sure that the full Url is exactly same as hard-coded value – BNK Mar 14 '17 at 04:06
  • Your URL has 2 id, 2 accessId and 2 currentUser parameter. Set your `Navigation_URL` to `http://192.168.100.5:84/api/academics/student` only. – BNK Mar 14 '17 at 04:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137973/discussion-between-seon-and-bnk). – seon Mar 14 '17 at 04:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137974/discussion-between-seon-and-bnk). – seon Mar 14 '17 at 05:14
1

hi use below code hope it will fix your problem

public void getDataFromServer(final Context context, String url, Integer method, final Map<String, String> params, final String sector ,final String AaccessToken ) {
    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest myReq = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            },
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("access_token", "your access token");

            return params;
        }

        protected Map<String, String> getParams()
                throws com.android.volley.AuthFailureError {
            return params;
        };


    };



    myReq.setRetryPolicy(new DefaultRetryPolicy(15000,
            1,
            1));
    queue.add(myReq);
}