Search code examples
androidfacebooksdkandroid-anr-dialognetworkonmainthread

Easy Facebook Android SDK - NetworkOnMainThreadException


I have been Using this SDK http://www.easyfacebookandroidsdk.com/ to integrate Facebook in my App. I only want to update a statur from my App. It worked great using this class in Android API 10. Running it on phone with API 15 gives me a android.os.NetworkOnMainThreadException. here my code:

package com.my.wod;

import android.app.Activity;
import android.content.Context;
import com.main.xfit.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;


import com.facebook.android.*;

import com.facebook.android.Facebook.DialogListener;

public class PostFaceBookStatus extends Activity {


private static final String APP_ID = "ID OF MY APP";
private static final String[] PERMISSIONS = {"publish_stream" };

private static final String TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebook = new Facebook(APP_ID);
    restoreCredentials(facebook);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.facebook);

    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    Toast.makeText(getApplicationContext(), facebookMessage, Toast.LENGTH_LONG).show();
    if (facebookMessage == null){
         facebookMessage = "SUNDAY 120506 2012 CrossFit Games Regional: Individual Event 4 For time: 135 pound Back squat, 50 reps 40 Pull-ups 135 pound Shoulder-to-overhead, 30 reps 85 pound Front squat, 50 reps 40 Pull-ups 85 pound Shoulder-to-overhead, 30 reps 65 pound Overhead squat, 50 reps 40 Pull-ups 65 pound Shoulder-to-overhead, 30 reps";
    }
    //messageToPost = facebookMessage;
    messageToPost = "Post random shit" ;
}

public void doNotShare(View button){
    finish();
}
public void share(View button){
    if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
            parameters.putString("message", message);
            parameters.putString("description", "topic share");
            try {
                facebook.request("me");
                String response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") ||
                response.equals("false")) {
                showToast("Blank response.");
                }
            else {
                showToast("Message posted to your facebook wall!");
                }
                finish();

            } catch (Exception e) {
                showToast("Failed to post to wall!");
                e.printStackTrace();
                finish();
    }
}

class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        finish();
    }
}

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}

Any ideas?


Solution

  • You can't run network operations on the UI thread, and if you think it is is the API code doing it, then you'll have to wrap the API call's in a Thread.

    Looks like this line is using the network:

     String response = facebook.request("me/feed", parameters, "POST");
    

    You need to do long running tasks on a worker thread.

    You used to get an ANR (Activity Not Responsding) but ICS is a little cleverer and kicks you off before this happens.

    Get reading:

    http://developer.android.com/guide/practices/responsiveness.html

    http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask < This is one possible alternative answer