這是我簡單的用戶登錄asynk任務,我已存儲的響應PARAMS在共享偏好,的Android客戶登錄
class logintask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
pdilog = new ProgressDialog(LoginActivity.this);
pdilog.setMessage("Please Wait....");
pdilog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
JSONObject jsonObject;
try {
response = LoginPostHit.getJSONfromURL(params[0], params[1]);
JSONObject _jobject = new JSONObject(response.toString());
customerid = _jobject.getString("CustomerID");
serverresponse = _jobject.getString("Result");
emailaddress=_jobject.getString("EmailAddress");
customername = _jobject.getString("CustomerName");
mobilenumber = _jobject.getString("MobileNumber");
} catch (JSONException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
pdilog.dismiss();
super.onPostExecute(result);
if (serverresponse.equals("Success")) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(AppConstants.VERIFICATION, MODE_PRIVATE).edit();
editor.putString(AppConstants.LOGGEDIN, "1");
editor.putString(AppConstants.CUSTOMERID, customerid);
editor.putString(AppConstants.CUSTOMERNAME, customername);
editor.putString(AppConstants.CUSTOMERMOBILENUMBER, mobilenumber);
editor.putString(AppConstants.CUSTOMEREMAIL,emailaddress);
editor.commit();
i.putExtra("CustomerName",customername);
startActivity(i);
} else {
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.wrongusrnamepass);
//dialog.setTitle("Wrong Credentials");
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Incorrect Email/Password Combination");
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
}
}
,這是我的Facebook登錄方法,在這裏我也已經存儲在該值同樣的共享偏好
lginbtn = (LoginButton) findViewById(R.id.tvfacebook);
lginbtn.setReadPermissions("public_profile", "email");
//LoginManager.getInstance().logOut();
lginbtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
if (AccessToken.getCurrentAccessToken() != null) {
Log.d("Accesstoken", AccessToken.getCurrentAccessToken().getToken());
RequestData(AccessToken.getCurrentAccessToken());
//i = new Intent(LoginActivity.this, MainActivity.class);
//startActivity(i);
}
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Login attempt Cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException error) {
//infomation.setText("Login attempt failed.");
Toast.makeText(LoginActivity.this, "Login attempt failed", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
public void RequestData(AccessToken accesstoken) {
GraphRequest request = GraphRequest.newMeRequest(accesstoken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
String firstname = "";
String lastname = "";
json = response.getJSONObject();
try {
if (json != null) {
JSONObject jsonPhoto = null;
JSONObject jsonPhotoo = null;
Session session = null;
// String text = "<b>FirstName :</b> " + json.getString("first_name") + "<b>LastName :</b> " + json.getString("last_name") + "<br><br><b>Email :</b> " + json.getString("email") + "<br><br><b>Profile link :</b> " + json.getString("link");
email = json.getString("email");
name = json.getString("name");
first_name = json.optString("first_name");
last_name = json.optString("last_name");
picture = json.getString("picture");
JSONObject jsonPhoto1 = json.getJSONObject("picture");
if (jsonPhoto1.has("data")) {
jsonPhoto = jsonPhoto1.getJSONObject("data");
url = jsonPhoto.getString("url");
}
new fblogintask().execute(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email,picture");
request.setParameters(parameters);
request.executeAsync();
}
class fblogintask extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
if (serverresponse.equals("Success")) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(AppConstants.VERIFICATION, MODE_PRIVATE).edit();
editor.putString(AppConstants.LOGGEDIN, "1");
editor.putString(AppConstants.CUSTOMERID, customerid);
editor.putString(AppConstants.CUSTOMERNAME, customername);
editor.putString(AppConstants.CUSTOMERMOBILENUMBER, mobilenumber);
editor.putString(AppConstants.CUSTOMEREMAIL,emailaddress);
startActivity(i);
} else
{
}
}
@Override
protected String doInBackground(String... voids) {
JSONObject jsonObject;
try {
response = FbLoginPostHit.getJSONfromURL(first_name, last_name, email, voids[0]);
JSONObject _jobject = new JSONObject(response.toString());
customerid = _jobject.getString("CustomerID");
serverresponse = _jobject.getString("Result");
emailaddress=_jobject.getString("EmailAddress");
customername = _jobject.getString("CustomerName");
mobilenumber = _jobject.getString("MobileNumber");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
現在的問題是,在第一種情況下,當我從那麼第二個活動的共享 PREF獲得價值則傳遞值,但是當我收到來自切絲首選項值在第二情況下,它是null ..我想要的用戶是用fb登錄還是簡單登錄。我在下一個活動中獲得所需的參數,有人建議我如何去做
這是我如何獲得其他活動的值customername = prefs.getString(AppConstants.CUSTOMERNAME,「」); – neha
你有沒有驗證迴應有關鍵和values.Like響應有「CustomerID」標記或沒有 – Rahul
是的它有客戶ID,名稱等所有的值 – neha