我无法将access_token发送到标头。我有登录系统,成功登录后会生成access_token。然后我必须将access_token发送到标头。我收到了意外的响应代码404.How问题怎么回事?解决了吗?
SessionManagement
public class SessionManagement {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_access_token = "access_token";
public static final String KEY_token_type = "token_type";
public static final String Key_EXPIRES_IN = "expires_in";
public static final String KEY_USERNAME = "userName";
public static final String KEY_MASTER_ID = "MasterID";
public static final String KEY_Name = "Name";
public static final String KEY_Access = "Access";
public static final String KEY_Issued = ".issued";
public static final String KEY_expires = ".expires";
String user_new_access_token;
// Constructor
public SessionManagement(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String accesstoken, String tokentype, String expiresin, String username, String masterId, String name, String access, String issued, String expires) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_access_token, accesstoken);
editor.putString(KEY_token_type, tokentype);
editor.putString(Key_EXPIRES_IN, expiresin);
editor.putString(KEY_USERNAME, username);
editor.putString(KEY_MASTER_ID, masterId);
editor.putString(KEY_Name, name);
editor.putString(KEY_Access, access);
editor.putString(KEY_Issued, issued);
editor.putString(KEY_expires, expires);
editor.apply();
String user_new_access_token = pref.getString(KEY_access_token, null);
String user_new_access_tokentype = pref.getString(KEY_token_type, null);
String user_name_expiresin = pref.getString(Key_EXPIRES_IN, null);
String user_name_Username = pref.getString(KEY_USERNAME, null);
String user_name_masterID = pref.getString(KEY_MASTER_ID, null);
String user_name_name = pref.getString(KEY_Name, null);
String user_name_access = pref.getString(KEY_Access, null);
String user_name_issued = pref.getString(KEY_Issued, null);
String user_name_expires = pref.getString(KEY_expires, null);
Log.d("TAG", "Access Token :" + accesstoken + user_new_access_token);
Log.d("TAG", "TokenType:" + user_new_access_tokentype);
Log.d("TAG", "Expires in:" + user_name_expiresin);
Log.d("TAG", "UserName:" + user_name_Username);
Log.d("TAG", "MasterID:" + user_name_masterID);
Log.d("TAG", "Name:" + user_name_name);
Log.d("TAG", "Access:" + user_name_access);
Log.d("TAG", "Issued:" + user_name_issued);
Log.d("TAG", "Expires:" + user_name_expires);
// String user_name_new = pref.getString(KEY_access_token, null);
// Log.d("TAG", " :" + accesstoken + " user_name_new:" + user_name_new);
// Log.d(tokentype, "admin");
//ad Log.d(expiresin, "expiresin");
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
*/
public void checkLogin() {
// Check login status
if (!this.isLoggedIn()) {
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
// user name
// user.put(KEY_USERNAME, pref.getString(KEY_USERNAME, null));
user.put(KEY_access_token, pref.getString(KEY_access_token, null));
user.put(KEY_token_type, pref.getString(KEY_token_type, null));
// user.put(KEY_TOKEN_TYPE, pref.getString(KEY_TOKEN_TYPE, null));
// user.put(KEY_MASTER_ID, pref.getString(KEY_MASTER_ID, null));
// user.put(KEY_access_token, pref.getString(KEY_access_token, null));
// user.put(KEY_NAME, pref.getString(KEY_NAME, null));
//user.put(KEY_Access, pref.getString(KEY_Access, null));
// return user
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
editor.clear();
editor.commit();
// After logout redirect user to Loing Activity
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
public String getMasterId() {
String masterID = pref.getString(KEY_MASTER_ID, null);
return masterID;
}
public String getAccess() {
String accessID = pref.getString(KEY_Access, null);
return accessID;
}
public String getAccesstToken()
{
String user_new_access_token = pref.getString(KEY_access_token, null);
return user_new_access_token;
}
public void clear() {
Log.d("TAg", "Full Cleared");
editor.clear();
// editor.remove(KEY_MASTER_ID);
// editor.remove(KEY_USERNAME);
editor.commit();
}
/**
* Quick check for login
**/
// Get Login State
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
家庭课程
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/";
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;
public void makeJsonObjectRequest() {
final 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();
// Toast.makeText(Home.this, "Why this error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() throws 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 requestQueue = Volley.newRequestQueue(this);
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);
}
}
}
我收到了意外的响应代码404.如何解决这个问题?