当我运行一行代码来启动一个活动时,我在logcat中得到以下打印输出,并启动了几个相同的活动?有人可以帮忙吗?
我的代码是......
public void login()
{
System.out.println("1");
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
@Override
public void onAuthSucceed() {
facebookConnector.idnum();
checkdatabase();
if(id == ""){
Intent i = new Intent(facebook.this, signup.class);
startActivity(i);
finish();
}
else{
System.out.println("TEST");
Intent i = new Intent(facebook.this, mainMenu.class);
startActivity(i);
finish();
}
}
@Override
public void onAuthFail(String error) {
System.out.println("2");
}
};
System.out.println("3");
SessionEvents.addAuthListener(listener);
System.out.println("4");
facebookConnector.login();
}
后跟checkdatabase方法......
public void checkdatabase(){
fbid = FacebookConnector.id;
System.out.println("facebook id in check database = " + fbid);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("fbid",fbid));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("checkdatabase.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
System.out.println("---------");
System.out.println(is);
}catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try
{
System.out.println("WSSSSSSSSSSSSSS");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
System.out.println(reader + "TEST");
StringBuilder sb = new StringBuilder();
System.out.println("Wmmmmmmmmmmmmmmm");
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
System.out.print("SAD");
System.out.println(result + "READER");
JSONArray jArray = new JSONArray(result);
JSONObject json_data = jArray.getJSONObject(1);
id = json_data.getString("facebookid");
System.out.println("facebooooooook id: " + fbid);
System.out.println("database id: " + id);
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
}
FacebookConnector Class
公共类FacebookConnector {private Facebook facebook = null;
private Context context;
private String[] permissions;
private Handler mHandler;
private Activity activity;
static String id;
private SessionListener mSessionListener = new SessionListener();;
public FacebookConnector(String appId,Activity activity,Context context,String[] permissions) {
this.facebook = new Facebook(appId);
id ="";
SessionStore.restore(facebook, context);
SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener(mSessionListener);
this.context=context;
this.permissions=permissions;
this.mHandler = new Handler();
this.activity=activity;
}
public void login() {
System.out.println("5");
// if (!facebook.isSessionValid()) {
facebook.authorize(this.activity, this.permissions,new LoginDialogListener());
// }
}
public void logout() {
System.out.println("33");
SessionEvents.onLogoutBegin();
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
asyncRunner.logout(this.context, new LogoutRequestListener());
}
public void idnum(){
try {
JSONObject jObject = null;
try {
jObject = new JSONObject(facebook.request("me"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
id =jObject.getString("id");
System.out.println("facebook id: " + id);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void postMessageOnWall(String msg) {
if (facebook.isSessionValid()) {
Bundle parameters = new Bundle();
parameters.putString("message", msg);
try {
String response = facebook.request("me/feed", parameters,"POST");
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
} else {
login();
}
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
}
public class LogoutRequestListener extends BaseRequestListener {
public void onComplete(String response, final Object state) {
// callback should be run in the original thread,
// not the background thread
mHandler.post(new Runnable() {
public void run() {
SessionEvents.onLogoutFinish();
}
});
}
}
private class SessionListener implements AuthListener, LogoutListener {
public void onAuthSucceed() {
SessionStore.save(facebook, context);
}
public void onAuthFail(String error) {
}
public void onLogoutBegin() {
}
public void onLogoutFinish() {
SessionStore.clear(context);
}
}
public Facebook getFacebook() {
System.out.println("55");
return this.facebook;
}
}
答案 0 :(得分:0)
您的代码不清楚,但如果顶部代码是您的“facebookConnector”类,则每次调用login时,都会再次添加侦听器,然后再次递归调用login。
答案 1 :(得分:0)
将日志添加到Authenticate并确保仅调用一次。我从那里开始......