Return Volley Result to a different class - java

how can I get the result of a StringRequest or JsonObjectRequest back to the class where I called the method.
In my MainActivty I have a Login Button. When I press it, it Calls the function checkLogin with a StingRequest from my Backgroundworker.class. How can I get the response back to my MainActivity to process it or Display it?
I can’t use a return at the end of the method because its asynchrony. I tried calling a method in my MainActivity but I always get an error when reaching the Intent
2019-05-16 16:43:52.794 3158-3158/com.test.Test E/Error: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
What’s the proper way to do a login function like this?
-MainActivity
package com.test.Test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.test.Test.tools.BackgroundWorker;
public class MainActivity extends AppCompatActivity {
private EditText txtEditUser;
private EditText txtEditPw;
private Button btnLogin;
private Button btnRegister;
private TextView txtViewLoginfailed;
private String user;
private String password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEditUser = findViewById(R.id.txtEditUser);
txtEditPw = findViewById(R.id.txtEditPw);
btnLogin = findViewById(R.id.btnLogin);
btnRegister = findViewById(R.id.btnRegister);
txtViewLoginfailed = findViewById(R.id.txtViewLoginfailed);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user = txtEditUser.getText().toString();
password = txtEditPw.getText().toString();
new BackgroundWorker().checkLogin(user, password, getApplicationContext());
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent register = new Intent(getApplicationContext(), Register.class);
startActivity(register);
}
});
}
public void Login(Boolean LoginOK,Context context) {
if (LoginOK) {
Intent menu = new Intent(context, Menu.class);
startActivity(menu);
} else {
txtViewLoginfailed.setText("Login failed");
}
}
}
-Backgroundworker
public class BackgroundWorker {
private Boolean LoginOK;
public void checkLogin(final String user, final String password, final Context context) {
String url = "http://192.168.0.2:80/webapp/login.php";
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Log.d("testLogin", "3");
if (response.equals("login success")) {
LoginOK = true;
new MainActivity().Login(LoginOK, context);
} else {
Log.d("testLogin", "5");
}
} catch (Exception exc) {
exc.printStackTrace();
Log.e("Error", exc.getMessage());
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_name", user);
params.put("password", password);
return params;
}
};
queue.add(sr);
}
}

What you need is Callback interface.
Create new interface e.g. LoginRequestCallback:
public interface LoginRequestCallback {
void onLoginRequestSuccess();
void onLoginRequestError(String errorMessage);
}
In your checkLogin method add new parameter LoginRequestCallback callback:
public void checkLogin(final String user, final String password, final Context context, LoginRequestCallback callback) { ... }
And then call method corresponding to either success of login or to the error:
public void checkLogin(final String user, final String password, final Context context) {
String url = "http://192.168.0.2:80/webapp/login.php";
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
Log.d("testLogin", "3");
if (response.equals("login success")) {
callback.onLoginRequestSuccess();
} else {
callback.onLoginRequestError("Invalid credentials");
}
} catch (Exception exc) {
exc.printStackTrace();
Log.e("Error", exc.getMessage());
callback.onLoginRequestError(exc.getMessage());
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
callback.onLoginRequestError(exc.getMessage());
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_name", user);
params.put("password", password);
return params;
}
};
queue.add(sr);
}
When calling from MainActivity, make MainActivity class implement this interface:
public class MainActivity extends Activity implements LoginRequestCallback {
//...
#Override
public void onLoginRequestSuccess() {
}
#Override
public void onLoginRequestError(String errorMessage) {
}
}
And when you call this method, pass this as callback:
void onLoginButtonClicked(View v) {
String user = "Asd";
String password = "asdasd";
this.backgroundWorker.checkLogin(user, password, this, this);
}
#Override
public void onLoginRequestSuccess() {
this.Login(true, this);
}
#Override
public void onLoginRequestError(String errorMessage) {
Toast.makeText(this, errorMessage, Toast.SHORT).show();
}

Related

Android Studio SharedPrerefernces is not working another activity

I have created two activities SignUp and Login.
Also, I have SharedPreferences which get edittext value. When I call SharedPreferences(getNomre) inside onCreate It works but doesn't work as global variable. I want to declare a variable named mobileNomre for using put to Constant.mobile. How can I send Shared Preference value to Constant.mobile?
p.s: Acitivity code is too large. I noted some parts of them.
Thanks
SignUpActivity:
public void SignUpWithEmail(View view) {
if (!validateForm()) {
return;
}
showProgressDialog();
final String email = edtEmail.getText().toString();
final String password = edtPassword.getText().toString();
final String name = edtName.getText().toString();
final String mobile = edtMobile.getText().toString();
Session.setNomre(getApplicationContext(),"nomreler",mobile);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(name).build();
assert user != null;
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
}
}
});
sendEmailVerification();
} else {
hideProgressDialog();
try {
throw Objects.requireNonNull(task.getException());
} catch (FirebaseAuthInvalidCredentialsException | FirebaseAuthInvalidUserException | FirebaseAuthUserCollisionException invalidEmail) {
inputEmail.setError(invalidEmail.getMessage());
} catch (Exception e) {
e.printStackTrace();
inputEmail.setError(e.getMessage());
}
}
}
});
}
LoginActivity:
public class LoginActivity extends AppCompatActivity {
String TAG = "LoginActivity";
int RC_SIGN_IN = 9001;
CallbackManager mCallbackManager;
String token;
String mobileNomre = Session.getNomre(getApplicationContext(),"nomreler","");
public static FirebaseAuth mAuth;
GoogleSignInClient mGoogleSignInClient;
TextView tvPrivacy, tvTest;
ProgressDialog mProgressDialog;
public TextInputEditText edtEmail, edtPassword;
public TextInputLayout inputEmail, inputPass;
public void UserSignUpWithSocialMedia( final String mobileNomre,
final String fCode,
final String referCode,
final String name,
final String email,
final String profile,
final String type) {
StringRequest strReq = new StringRequest(Request.Method.POST, Constant.QUIZ_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
if (obj.getString("error").equals("false")) {
JSONObject jsonobj = obj.getJSONObject("data");
if (!jsonobj.getString(Constant.status).equals(Constant.DE_ACTIVE)) {
Session.saveUserDetail(getApplicationContext(),
jsonobj.getString(Constant.userId),
jsonobj.getString(Constant.name),
jsonobj.getString(Constant.email),
jsonobj.getString(Constant.mobile),
jsonobj.getString(Constant.PROFILE), referCode);
User user = new User(
jsonobj.getString(Constant.mobile),
jsonobj.getString(Constant.name),
jsonobj.getString(Constant.email),
"0",
false,
jsonobj.getString(Constant.PROFILE),
"0",
token,
jsonobj.getString(Constant.userId));
FirebaseDatabase.getInstance().getReference("user").child(FirebaseAuth.getInstance().
getCurrentUser().getUid()).setValue(user).
addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task1) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("type", "default");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
});
hideProgressDialog();
} else
setSnackBarStatus();
} else {
LoginManager.getInstance().logOut();
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(Constant.accessKey, Constant.accessKeyValue);
params.put(Constant.userSignUp, "1");
params.put(Constant.email, email);
params.put(Constant.name, name);
params.put(Constant.PROFILE, profile);
params.put(Constant.fcmId, token);
params.put(Constant.type, type);
params.put(Constant.mobile, mobileNomre );
params.put(Constant.REFER_CODE, referCode);
params.put(Constant.FRIENDS_CODE, fCode);
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
params.put(Constant.ipAddress, ip);
System.out.println("---params social " + params.toString());
return params;
}
};
// AppController.getInstance().getRequestQueue().getCache().clear();
AppController.getInstance().addToRequestQueue(strReq);
}
SharedPrereferences:
public class Session {
public static void setNomre(Context mContext, String key, String objString) {
SharedPreferences.Editor editor =
mContext.getSharedPreferences(mContext.getString(R.string.app_name),
Context.MODE_PRIVATE).edit();
editor.putString(key, objString);
editor.commit();
}
public static String getNomre(Context mContext, final String key, final String defaultStr) {
SharedPreferences pref = mContext.getSharedPreferences(mContext.getString(R.string.app_name),
Context.MODE_PRIVATE);
return pref.getString(key, defaultStr);
}}
Make your String public static to access it globally;
public static String mobileNomre = Session.getNomre(getApplicationContext(),"nomreler","");
Haven't tried but working around with below code can give you an idea,
We uses this method if we want to access any function of Activity1 in Activity2..
AnotherActivity anotheractivity = (AnotherActivity) this.getActivity();

Sinch not starting

This is such a basic issue that I am not sure what I could possibly be doing wrong. Sinch is not starting for me and I don't know why. I don't have enough experience with Sinch to diagnose why a basic command is not doing what it is supposed to do. Here's what I have:
I am trying to start and making the call from the Calling.java class. The code is as follows:
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.sinch.android.rtc.calling.Call;
import com.squareup.picasso.Picasso;
import static android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP;
public class Calling extends CallActivity {
private String calleeID;
private TextView serviceName;
Bundle callDetails;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calling);
callDetails = getIntent().getBundleExtra("callDetails");
//Setup end button
Button endCallButton = findViewById(R.id.endcall);
endCallButton.setOnClickListener(v -> endCall());
}
private void endCall() {
if (getSinchServiceInterface() != null) {
getSinchServiceInterface().stopClient();
}
finishActivity(FLAG_ACTIVITY_PREVIOUS_IS_TOP);
finish();
}
// invoked when the connection with SinchServer is established
#Override
protected void onServiceConnected() {
//Setup Calling Screen
ImageView avatar = findViewById(R.id.dialingAvatar);
Picasso.get().load(callDetails.getString("Logo")).into(avatar);
TextView midScreenName = findViewById(R.id.memberName);
midScreenName.setText(callDetails.getString("Name"));
serviceName = findViewById(R.id.serviceName);
serviceName.setText(callDetails.getString("Service"));
TextView ratings = findViewById(R.id.rating);
ratings.setText(callDetails.getString("Rating") + " ★");
//Get CallerID and CalleeID
calleeID = callDetails.getString("CalleeID");
//Start sinch Service
if(!getSinchServiceInterface().isStarted()){
getSinchServiceInterface().startClient(callDetails.getString("CallerID"));
Call call = getSinchServiceInterface().callUserVideo(calleeID);
Intent callServiceScreen = new Intent(this, ServiceCallActivity.class);
callDetails.putString(SinchService.CALL_ID, call.getCallId());
callServiceScreen.putExtra("Call Details", callDetails);
startActivity(callServiceScreen);
}
}
#Override
public void onDestroy() {
if (getSinchServiceInterface() != null) {
getSinchServiceInterface().stopClient();
}
super.onDestroy();
}
}
I am coming to Calling.java from Precall.java the code for that is:
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.sinch.android.rtc.SinchError;
import com.squareup.picasso.Picasso;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class precall extends CallActivity implements SinchService.StartFailedListener {
private Bundle memberDetails;
private String url;
private Button cancel;
private Button call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_precall);
//url
url = apiCallPoints.userInfo;
//Set Member Text
memberDetails = getIntent().getBundleExtra("Member");
//Populate screen
ImageView avatar = findViewById(R.id.avatar);
Picasso.get().load(memberDetails.getString("Logo")).into(avatar);
TextView memberName = findViewById(R.id.membername);
memberName.setText(memberDetails.getString("Name"));
TextView rating = findViewById(R.id.rating);
rating.setText(memberDetails.getString("Rating") + " ★");
TextView serviceName = findViewById(R.id.servicename);
serviceName.setText(memberDetails.getString("Service"));
TextView overview = findViewById(R.id.overview);
overview.setText(memberDetails.getString("Overview"));
//Add button clicks
cancel = findViewById(R.id.cancel_button);
cancel.setOnClickListener(view -> finish());
cancel.setEnabled(false);
call = findViewById(R.id.yes_button);
call.setOnClickListener(view -> {
goToCalling();
});
call.setEnabled(false);
setHomeBar();
}
//this method is invoked when the connection is established with the SinchService
#Override
protected void onServiceConnected() {
call.setEnabled(true);
cancel.setEnabled(true);
getSinchServiceInterface().setStartListener(this);
}
#Override
protected void onPause() {
super.onPause();
}
#Override
public void onStartFailed(SinchError error) {
}
//Invoked when just after the service is connected with Sinch
#Override
public void onStarted() {
}
private void goToCalling() {
//Async search
CallBackendSync callBackendSync = new CallBackendSync();
Object [] params = {url, memberDetails};
callBackendSync.execute(params);
}
private void setHomeBar() {
final Button home = findViewById(R.id.home_button);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
startActivity(new Intent(precall.this, SecondActivity.class));
}
});
final Button favourites = findViewById(R.id.star_button);
favourites.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
startActivity(new Intent(precall.this, Favourite_Page.class));
}
});
final Button profile_page = findViewById(R.id.person_button);
profile_page.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
startActivity(new Intent(getApplicationContext(), Profile.class));
}
});
final Button notifications = findViewById(R.id.notification_button);
notifications.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
startActivity(new Intent(precall.this, Notification_Page.class));
}
});
final Button service = findViewById(R.id.service_button);
service.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
startActivity(new Intent(precall.this, services.class));
}
});
}
class CallBackendSync extends AsyncTask {
OkHttpClient client = new OkHttpClient();
#Override
protected Object doInBackground(Object [] objects) {
String url = (String) objects[0];
Bundle memberDetails = (Bundle) objects[1];
//Get access token from shared preference
isLoggedIn loggedIn = new isLoggedIn(getApplicationContext());
String token = loggedIn.getToken();
if(token != null){
//Create request
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", "Bearer " + token)
.addHeader("Accept", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
JSONObject results = new JSONObject(response.body().string());
String UserID = results.getString("UserId");
memberDetails.putString("CallerID", UserID);
Intent callIntent = new Intent(precall.this, Calling.class);
callIntent.putExtra("callDetails", memberDetails);
startActivity(callIntent);
return results;
}catch (Exception e){
e.printStackTrace();
}
} else {
startActivity(new Intent(precall.this, Login_page.class));
}
return null;
}
protected void onPostExecute(String s){
super.onPostExecute(s);
}
}
}
The failure is happening in SinchService.java
import com.sinch.android.rtc.AudioController;
import com.sinch.android.rtc.ClientRegistration;
import com.sinch.android.rtc.Sinch;
import com.sinch.android.rtc.SinchClient;
import com.sinch.android.rtc.SinchClientListener;
import com.sinch.android.rtc.SinchError;
import com.sinch.android.rtc.video.VideoController;
import com.sinch.android.rtc.calling.Call;
import com.sinch.android.rtc.calling.CallClient;
import com.sinch.android.rtc.calling.CallClientListener;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class SinchService extends Service {
private static final String APP_KEY = "is correct";
private static final String APP_SECRET = "is correct";
//private static final String ENVIRONMENT = "clientapi.sinch.com";
private static final String ENVIRONMENT = "sandbox.sinch.com";
public static final String CALL_ID = "CALL_ID";
static final String TAG = SinchService.class.getSimpleName();
private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface();
private SinchClient mSinchClient = null;
private String mUserId = "";
private StartFailedListener mListener;
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
if(mSinchClient != null){
mSinchClient.terminate();
}
super.onDestroy();
}
private void start(String userName) {
mUserId = userName;
mSinchClient = Sinch.getSinchClientBuilder().context(getApplicationContext())
.applicationKey(APP_KEY)
.applicationSecret(APP_SECRET)
.environmentHost(ENVIRONMENT)
.userId(userName)
.enableVideoCalls(true)
.build();
mSinchClient.setSupportCalling(true);
mSinchClient.startListeningOnActiveConnection();
mSinchClient.addSinchClientListener(new MySinchClientListener());
mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
mSinchClient.checkManifest();
mSinchClient.start();
System.out.println("Is started: " + mSinchClient.isStarted());
}
private void stop() {
if(mSinchClient != null){
mSinchClient.terminate();
}
}
private boolean isStarted() {
if(mSinchClient != null){
return mSinchClient.isStarted();
} else {
return false;
}
}
#Override
public IBinder onBind(Intent intent) {
return mSinchServiceInterface;
}
public class SinchServiceInterface extends Binder {
public Call callUserVideo(String userId) {
return mSinchClient.getCallClient().callUserVideo(userId);
}
public String getUserName() {
return mUserId;
}
public boolean isStarted() {
return SinchService.this.isStarted();
}
public void startClient(String userName) {
start(userName);
}
public void stopClient() {
stop();
}
public void setStartListener(StartFailedListener listener) {
mListener = listener;
}
public Call getCall(String callId) {
return mSinchClient.getCallClient().getCall(callId);
}
public VideoController getVideoController() {
return mSinchClient.getVideoController();
}
public AudioController getAudioController() {
return mSinchClient.getAudioController();
}
}
public interface StartFailedListener {
void onStartFailed(SinchError error);
void onStarted();
}
private class MySinchClientListener implements SinchClientListener {
#Override
public void onClientFailed(SinchClient client, SinchError error) {
if (mListener != null) {
mListener.onStartFailed(error);
}
mSinchClient.terminate();
mSinchClient = null;
}
#Override
public void onClientStarted(SinchClient client) {
Log.d(TAG, "SinchClient started");
if (mListener != null) {
mListener.onStarted();
}
}
#Override
public void onClientStopped(SinchClient client) {
Log.d(TAG, "SinchClient stopped");
}
#Override
public void onLogMessage(int level, String area, String message) {
switch (level) {
case Log.DEBUG:
Log.d(area, message);
break;
case Log.ERROR:
Log.e(area, message);
break;
case Log.INFO:
Log.i(area, message);
break;
case Log.VERBOSE:
Log.v(area, message);
break;
case Log.WARN:
Log.w(area, message);
break;
}
}
#Override
public void onRegistrationCredentialsRequired(SinchClient client,
ClientRegistration clientRegistration) {
}
}
private class SinchCallClientListener implements CallClientListener {
#Override
public void onIncomingCall(CallClient callClient, Call call) {
Log.d(TAG, "Incoming call");
Intent intent = new Intent(SinchService.this, Calling.class);
intent.putExtra(CALL_ID, call.getCallId());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SinchService.this.startActivity(intent);
}
}
}
And the base activity is CallActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.sinch.android.rtc.calling.Call;
import com.squareup.picasso.Picasso;
import static android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP;
public class Calling extends CallActivity {
private String calleeID;
private TextView serviceName;
Bundle callDetails;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calling);
callDetails = getIntent().getBundleExtra("callDetails");
//Setup end button
Button endCallButton = findViewById(R.id.endcall);
endCallButton.setOnClickListener(v -> endCall());
}
private void endCall() {
if (getSinchServiceInterface() != null) {
getSinchServiceInterface().stopClient();
}
finishActivity(FLAG_ACTIVITY_PREVIOUS_IS_TOP);
finish();
}
// invoked when the connection with SinchServer is established
#Override
protected void onServiceConnected() {
//Setup Calling Screen
ImageView avatar = findViewById(R.id.dialingAvatar);
Picasso.get().load(callDetails.getString("Logo")).into(avatar);
TextView midScreenName = findViewById(R.id.memberName);
midScreenName.setText(callDetails.getString("Name"));
serviceName = findViewById(R.id.serviceName);
serviceName.setText(callDetails.getString("Service"));
TextView ratings = findViewById(R.id.rating);
ratings.setText(callDetails.getString("Rating") + " ★");
//Get CallerID and CalleeID
calleeID = callDetails.getString("CalleeID");
//Start sinch Service
if(!getSinchServiceInterface().isStarted()){
getSinchServiceInterface().startClient(callDetails.getString("CallerID"));
Call call = getSinchServiceInterface().callUserVideo(calleeID);
Intent callServiceScreen = new Intent(this, ServiceCallActivity.class);
callDetails.putString(SinchService.CALL_ID, call.getCallId());
callServiceScreen.putExtra("Call Details", callDetails);
startActivity(callServiceScreen);
}
}
#Override
public void onDestroy() {
if (getSinchServiceInterface() != null) {
getSinchServiceInterface().stopClient();
}
super.onDestroy();
}
}
I have been banging my head against this but I cannot figure out what's wrong. I sure it's something stupid and obvious that I can't see because I am too close to the code. But any ideas you guys have would be very helpful!
It looks like you are not waiting for it to start before you try to make a call. We recommend to start the service when the app starts. If you dont do that you need to wait or the onStarted event in the service for fire

W/System.err: org.json.JSONException: No value for email.. got this error when i parsing login data through volley

when i put my username and password on login page. then i got api response but my dashboard activity not open. i am getting W/System.err: org.json.JSONException: No value for email error on my api parameter. when i remove email parameter then this error jump to next parameter. what can i do
? need some help
I am tried many time to login , this api response are working but activity is not open.
LoginModel.java
package com.tsa.nccapp.models;
/**
* Created by hp on 31-Dec-2018.
*/
public class LoginModel {
private String regiment_number;
private String rank;
private String name_of_cadet;
private String email;
private String mobile;
private String father_name;
private String exam_type;
private String unit;
private String ncc_group;
private String state;
private String district;
private String town;
private String directorate;
private String membership_status;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName_of_cadet() {
return name_of_cadet;
}
public void setName_of_cadet(String name_of_cadet) {
this.name_of_cadet = name_of_cadet;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public String getExam_type() {
return exam_type;
}
public void setExam_type(String exam_type) {
this.exam_type = exam_type;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getNcc_group() {
return ncc_group;
}
public void setNcc_group(String ncc_group) {
this.ncc_group = ncc_group;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getDirectorate() {
return directorate;
}
public void setDirectorate(String directorate) {
this.directorate = directorate;
}
public String getRegiment_number() {
return regiment_number;
}
public void setRegiment_number(String regiment_number) {
this.regiment_number = regiment_number;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
}
LoginActivity.java
package com.tsa.nccapp;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
import com.tsa.nccapp.Network.NetworkCheck;
import com.tsa.nccapp.custom.CustomActivity;
import com.tsa.nccapp.custom.RightDrawableOnTouchListener;
import com.tsa.nccapp.models.LoginModel;
import com.tsa.nccapp.models.UserModel;
import com.tsa.nccapp.utils.GLOBAL;
import com.tsa.nccapp.validation.Validation;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Hashtable;
import java.util.Map;
/********************************************************
* A login screen that offers login via email/password. *
********************************************************/
public class LoginActivity2 extends CustomActivity {
private static final int RC_SIGN_IN = 1;
private static Context context;
private EditText username;
private EditText password;
private boolean pwdFlag = false;
private GoogleSignInClient mGoogleSignInClient;
SharedPreferences sharedPref;
LinearLayout mainRoot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
context = LoginActivity2.this;
sharedPref = context.getSharedPreferences(
"login", Context.MODE_PRIVATE);
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
init();
}
/////////////////////////
private void init() {
username = findViewById(R.id.user_name);
password = findViewById(R.id.password);
mainRoot=findViewById(R.id.main_root);
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (NetworkCheck.checkInternet(mainRoot,context))
signIn();
}
});
password.setOnTouchListener(new RightDrawableOnTouchListener(password) {
#Override
public boolean onDrawableTouch(final MotionEvent event) {
return onClickSearch(event);
}
});
}
//////////////////////////////////////////
public void goToDashboard(View view) {
if (checkValidation()) {
if (NetworkCheck.checkInternet(mainRoot,context)) {
login(username.getText().toString(), password.getText().toString());
}
}
}
///////////////////////////////////////////
public void goRegistration(View view) {
if (NetworkCheck.checkInternet(mainRoot,context)) {
startActivity(new Intent(LoginActivity2.this, Signup_oneActivity.class));
finish();
}
}
////////////////////////////////////////////////////////////////
public void login(final String username, final String password) {
//Showing the progress dialog
final ProgressDialog progress = new ProgressDialog(LoginActivity2.this);
progress.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, GLOBAL.baseURL+ "loginUser.php",
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
Log.d("Login", s.toString());
try {
JSONObject json = new JSONObject(s);
if (json.getString("status").equals("0")) {
GLOBAL.globalLoginModel = new LoginModel();
// GLOBAL.globalLoginModel.setName_of_cadet(json.getString("name_of_cadet"));
GLOBAL.globalLoginModel.setEmail(json.getString("email"));
GLOBAL.globalLoginModel.setMobile(json.getString("mobile"));
GLOBAL.globalLoginModel.setRegiment_number(json.getString("regiment_number"));
GLOBAL.globalLoginModel.setRank(json.getString("rank"));
GLOBAL.globalLoginModel.setFather_name(json.getString("father_name"));
GLOBAL.globalLoginModel.setExam_type(json.getString("exam_type"));
GLOBAL.globalLoginModel.setUnit(json.getString("unit"));
GLOBAL.globalLoginModel.setNcc_group(json.getString("ncc_group"));
GLOBAL.globalLoginModel.setState(json.getString("state"));
GLOBAL.globalLoginModel.setDistrict(json.getString("district"));
GLOBAL.globalLoginModel.setTown(json.getString("town"));
GLOBAL.globalLoginModel.setDirectorate(json.getString("directorate"));
GLOBAL.globalLoginModel.setUsername(json.getString("username"));
GLOBAL.globalLoginModel.setPassword(json.getString("password"));
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
GLOBAL.loginType=GLOBAL.USERLOGIN;
Log.d("Login", s.toString());
startActivity(new Intent(LoginActivity2.this, Main2Activity.class));
finish();
} else {
Log.d("Login", s.toString());
progress.dismiss();
Toast.makeText(context, "Invalid User Name or Password", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
progress.dismiss();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(context, "Some issue in loading" + volleyError, Toast.LENGTH_LONG).show();
progress.dismiss();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new Hashtable<String, String>();
params.put("username", username);
params.put("password", password);
params.put("status", "Login");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity2.this);
requestQueue.add(stringRequest);
}
////////////////////////////////////////////////////////////
private boolean onClickSearch(final MotionEvent event) {
// do something
pwdFlag = !pwdFlag;
if (pwdFlag) {
password.setInputType(InputType.TYPE_CLASS_TEXT);
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.off_eye, 0);
} else {
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.eye_20, 0);
}
event.setAction(MotionEvent.ACTION_CANCEL);
return false;
}
///////////////////////////////////////
private boolean checkValidation() {
boolean ret = true;
if (!Validation.hasText(password)) ret = false;
if (!Validation.hasText(username)) ret = false;
return ret;
}
///////////////////////////////////
public void goHome(View view) {
if(NetworkCheck.checkInternet(mainRoot,context)) {
GLOBAL.globalLoginModel = new LoginModel();
Log.e("json", GLOBAL.globalLoginModel.toString());
GLOBAL.globalLoginModel.setName_of_cadet("Guest User");
GLOBAL.globalLoginModel.setUsername("Guest User");
/*GLOBAL.loginType=GLOBAL.GUESTLOGIN;*/
startActivity(new Intent(LoginActivity2.this, Main2Activity.class));
finish();
}
}
///////////////////////////////
// #Override
// protected void onStart() {
// super.onStart();
// if (NetworkCheck.checkInternet(mainRoot,context)) {
// GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
// if (account != null) {
// GLOBAL.globalLoginModel = new LoginModel();
// GLOBAL.globalLoginModel.setUsername(account.getDisplayName());
// GLOBAL.globalLoginModel.setName_of_cadet(account.getDisplayName());
// GLOBAL.globalLoginModel.setEmail(account.getEmail());
// GLOBAL.photoURL = "" + account.getPhotoUrl();
// /* GLOBAL.loginType=GLOBAL.GMAILLOGIN;*/
// startActivity(new Intent(LoginActivity2.this, Main2Activity.class));
// finish();
// }
// }
// }
///////////////////////////
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
/////////////////////////////////////////////////////////////////////////////////
// #Override
// public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RC_SIGN_IN) {
// Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
// handleSignInResult(task);
// }
// }
////////////////////////////////////////////////////////////////////////////////
// private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
// try {
// GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// GLOBAL.globalLoginModel = new LoginModel();
// GLOBAL.globalLoginModel.setUsername(account.getDisplayName());
// GLOBAL.globalLoginModel.setName_of_cadet(account.getDisplayName());
// GLOBAL.globalLoginModel.setEmail(account.getEmail());
// GLOBAL.photoURL = "" + account.getPhotoUrl();
// registerWithGPlus();
// } catch (ApiException e) {
// Log.w("TAG", "signInResult:failed code=" + e.getStatusCode());
// }
//}
////////////////////////////////////////////////////////////////////////////////////////////////
public void goFargotPwd(View view) {
if (NetworkCheck.checkInternet(mainRoot,context)){
startActivity(new Intent(context, ForgetPasswordActivity.class));
finish();}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// public void registerWithGPlus() {
// //Showing the progress dialog
// final ProgressDialog progress = new ProgressDialog(context);
// progress.show();
// StringRequest stringRequest = new StringRequest(Request.Method.POST, GLOBAL.baseURL + "social_media_login.php",
// new Response.Listener<String>() {
// #Override
// public void onResponse(String s) {
// try {
// JSONObject json = new JSONObject(s);
// Log.e("j son", json.toString());
// progress.dismiss();
// /* GLOBAL.loginType=GLOBAL.GMAILLOGIN;*/
// startActivity(new Intent(LoginActivity2.this, Main2Activity.class));
// finish();
// } catch (JSONException e) {
// e.printStackTrace();
// progress.dismiss();
// }
// }
//
// },
// new Response.ErrorListener() {
// #Override
// public void onErrorResponse(VolleyError volleyError) {
// //Showing toast
// Toast.makeText(context, "Some issue in loading" + volleyError, Toast.LENGTH_LONG).show();
// progress.dismiss();
// }
// }) {
// #Override
// protected Map<String, String> getParams() throws AuthFailureError {
//
// //Creating parameters
// Map<String, String> params = new Hashtable<String, String>();
//
// //Adding parameters
// params.put("email", "" + GLOBAL.globalLoginModel.getEmail());
// params.put("name", "" + GLOBAL.globalLoginModel.getName_of_cadet());
// params.put("mobile", "" + GLOBAL.globalLoginModel.getMobile());
// params.put("state", "" + GLOBAL.globalLoginModel.getState());
// params.put("social_media", "Gmail");
//
// return params;
// }
// };
//Creating a Request Queue
// RequestQueue requestQueue = Volley.newRequestQueue(context);
//
// //Adding request to the queue
// requestQueue.add(stringRequest);
// }
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Really Exit?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
LoginActivity2.super.onBackPressed();
}
}).create().show();
}
}
Global.java
package com.tsa.nccapp.utils;
import com.tsa.nccapp.R;
import com.tsa.nccapp.models.CadetDetailsModels;
import com.tsa.nccapp.models.LoginModel;
import com.tsa.nccapp.models.OtherExamResultModel;
import com.tsa.nccapp.models.PasswordModel;
import com.tsa.nccapp.models.ReportModel;
import com.tsa.nccapp.models.UserModel;
import java.util.ArrayList;
import java.util.Stack;
/**
* Created by Akhil Tripathi on 28-12-2017.
*/
public class GLOBAL {
public final static String baseURL="http://internationalskills.co.in/nccdarpan/API/";
public static UserModel globalUserModel=null;
public static LoginModel globalLoginModel=null;
public static PasswordModel globalPasswordModel=null;
public static String url="";
public static String cert="A";
public static int xD=0;
public static int yD=0;
public static final int GMAILLOGIN=2;
public static final int GUESTLOGIN=1;
public static final int USERLOGIN=0;
public static int loginType=USERLOGIN;
public static String password;
public static String username;
public static int textColor= R.color.black;
public static int BGColor= R.color.light_green;
public static int textSize= 14;
public static int scrollAmount=0;
public static ArrayList<ReportModel> reportModels;
public static Stack<Integer> lastVisitedIndex=new Stack<>();
public static String photoURL="https://icons8.com/A.png";
public static ArrayList<OtherExamResultModel> otherExamResultModels=new ArrayList<>();
public static CadetDetailsModels cadetFormModel;
}

Autoclick saveButton in every change of string in editText

Receive SMS then set EditText to msgBody
public class SmsBroadcastReceiver extends BroadcastReceiver {
//.....
((EditText)MainActivity.mThis.findViewById(R.id.editTextName)).setText(msgBody);}
The error is this in View cannot be applied to android.view.View.Onclicklistiner
//onCreate
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttonSave.performClick(this);
}
});
the message will automatically save to SQLite and sync to Mysql when buttonSave is click
private void saveNameToServer() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Saving Name...");
progressDialog.show();
final String name = editTextName.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVE_NAME,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject obj = new JSONObject(response);
if (!obj.getBoolean("error")) {
//if there is a success
//storing the name to sqlite with status synced
saveNameToLocalStorage(name, NAME_SYNCED_WITH_SERVER);
} else {
//if there is some error
//saving the name to sqlite with status unsynced
saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
//on error storing the name to sqlite with status unsynced
saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", name);
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
#Override
public void onClick(View view) {
saveNameToServer();
}
Are there other ways to auto click button when EditText value changes?
Instead of invoking the click buttonSave.performClick(this); just simply invoke saveNameToServer(); method to save your data.
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//buttonSave.performClick(this); // remove, not required
saveNameToServer(); // save your data
}
});

How to get emailid from Facebook Login in using facebook sdk version 3.22.0

I am using only one button when i click on the button then i want to get the email id.
Here is my code and get the ID and name, but emailid cannot get.I am using facebook sdk version 3.22.0 Please help me.
enter code here
package com.example.testintegration;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.testfbintegration.R;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.model.GraphUser;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static String APP_ID = "862722850469774"; // Replace with your App ID
// Instance of Facebook Class
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
// Buttons
Button btnFbLogin;
/*Button btnFbGetProfile;
Button btnPostToWall;
Button btnShowAccessTokens;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFbLogin = (Button) findViewById(R.id.btn_fblogin);
/*btnFbGetProfile = (Button) findViewById(R.id.btn_get_profile);
btnPostToWall = (Button) findViewById(R.id.btn_fb_post_to_wall);
btnShowAccessTokens = (Button) findViewById(R.id.btn_show_access_tokens);*/
mAsyncRunner = new AsyncFacebookRunner(facebook);
/**
* Login button Click event
* */
btnFbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
//loginToFacebook();
logfacebook();
}
});
/**
* Getting facebook Profile info
* *//*
btnFbGetProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getProfileInformation();
}
});
*//**
* Posting to Facebook Wall
* *//*
btnPostToWall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postToWall();
}
});
*//**
* Showing Access Tokens
* *//*
btnShowAccessTokens.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAccessTokens();
}
});*/
}
public void logfacebook()
{
Session.openActiveSession(MainActivity.this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
System.out.println(user.getName());
System.out.println(user.getBirthday());
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getLink());
System.out.println(user.getUsername());
System.out.println(user.getLocation());
System.out.println("facebook user id" + user.getId());
System.out.println(user.asMap().get("email").toString());
// Session.OpenRequest open = new Session.OpenRequest(Login)
}
}
});
}
}
});
}
/**
* Function to login into facebook
* */
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
btnFbLogin.setVisibility(View.INVISIBLE);
// Making get profile button visible
// btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
//btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
//btnShowAccessTokens.setVisibility(View.VISIBLE);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_actions" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
// Making Login button invisible
btnFbLogin.setVisibility(View.INVISIBLE);
// Making logout Button visible
// btnFbGetProfile.setVisibility(View.VISIBLE);
// Making post to wall visible
//btnPostToWall.setVisibility(View.VISIBLE);
// Making show access tokens button visible
//btnShowAccessTokens.setVisibility(View.VISIBLE);
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
/**
* Get Profile information by making request to Facebook Graph API
* */
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
// Facebook Profile JSON data
JSONObject profile = new JSONObject(json);
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
/**
* Function to post to facebook wall
* */
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
/**
* Function to show Access Tokens
* */
public void showAccessTokens() {
String access_token = facebook.getAccessToken();
Toast.makeText(getApplicationContext(),
"Access Token: " + access_token, Toast.LENGTH_LONG).show();
}
/**
* Function to Logout user from Facebook
* */
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// make Login button visible
btnFbLogin.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
/*btnFbGetProfile.setVisibility(View.INVISIBLE);
btnPostToWall.setVisibility(View.INVISIBLE);
btnShowAccessTokens.setVisibility(View.INVISIBLE);*/
}
});
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}
use this code.
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() {
#Override
public void onSuccess(LoginResult result) {
new GraphRequest();
// SocialSdkPrefrences.getInstance().setAccessToken(result.getAccessToken().getToken());
GraphRequest request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Profile profile = Profile.getCurrentProfile();
FacebookGraphUser fbGraphUser = (FacebookGraphUser) JsonUtil.toModel(response.getRawResponse(), FacebookGraphUser.class);
if (fbGraphUser != null) {
String image_url = "http://graph.facebook.com/" + fbGraphUser.getId() + "/picture?type=large";
// UniversalImageLoaderUtil.loadImageWithDefaultImage(image_url, (ImageView) findViewById(R.id.user_image), null, R.drawable.place_holder_album);
User user=new User(fbGraphUser.getEmail(),image_url,profile.getName(),Long.parseLong(fbGraphUser.getId()));
Gson gson = new Gson();
Log.d("social", gson.toJson(user));
if (fbGraphUser.getEmail() != null) {
if (Util.isValidEmail(fbGraphUser.getEmail())) {
new SocialLogInAsyncTask(LoginActivity.this, user).execute();
} else {
Toast.showShortToast(LoginActivity.this, "You have not valid email,cant login");
}
} else {
Toast.showShortToast(LoginActivity.this, "You have not email,cant login");
}
} else {
Toast.showErrorToast(LoginActivity.this, R.string.error_signup);
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture.width(300)");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Log.d("tag", "cancel facebook login");
}
#Override
public void onError(FacebookException error) {
Log.d("tag", "error in login" + error.getMessage());
}
});
Please check this link of your answer & let me know if you find any problem in fb integration.
Unable to get emailId after login through fb in android
call this in your main Activity or the activity that is using FaceBookSignIn method
FacebookSdk.sdkInitialize(MainActivity.this);
add this method in your activity
public void FaceBookSignIn() {
callbackManager = CallbackManager.Factory.create();
loginType = Constants.loginTypeFaceBook;
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("email", "user_photos", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
DisplaySnackBar.display(getWindow().getDecorView().findViewById(android.R.id.content), "Success", true);
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
String email = jsonObject.getString(Constants.FacebookEmail),
String name = jsonObject.getString(Constants.FacebookName),
String firstName = jsonObject.getString(Constants.FacebookFirstName),
String lastName = jsonObject.getString(Constants.FacebookLastName),
String id = jsonObject.getString(Constants.FacebookID);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,birthday,email");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
DisplaySnackBar.display(getWindow().getDecorView().findViewById(android.R.id.content), "Sign In Cancel", true);
}
#Override
public void onError(FacebookException e) {
DisplaySnackBar.display(getWindow().getDecorView().findViewById(android.R.id.content), "Error", true);
}
});
}
Don't forget to add this dependency in your gradle build file
compile 'com.facebook.android:facebook-android-sdk:4.5.0'

Categories