1

我收到一个错误,不知道为什么会这样。

错误是:

01-31 16:55:43.000: D/AndroidRuntime(1067): Shutting down VM
01-31 16:55:43.190: E/AndroidRuntime(1067): java.lang.StackOverflowError
01-31 16:55:43.190: E/AndroidRuntime(1067):     at facebook.android.AsyncFacebookRunner.request(AsyncFacebookRunner.java:206)
01-31 16:55:43.190: E/AndroidRuntime(1067):     at facebook.android.AsyncFacebookRunner.request(AsyncFacebookRunner.java:206)
01-31 16:55:43.190: E/AndroidRuntime(1067):     at facebook.android.AsyncFacebookRunner.request(AsyncFacebookRunner.java:206)

这是代码:

package com.example.facebookintregation;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import facebook.android.AsyncFacebookRunner;
import facebook.android.DialogError;
import facebook.android.Facebook;
import facebook.android.Facebook.DialogListener;
import facebook.android.FacebookError;

public class ShareOnFacebook extends Activity {

    private static final String APP_ID = "360848087324012";
    private static final String[] PERMISSIONS = new String[] { "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;
    AsyncFacebookRunner asyncRunner;
    static Bitmap User_pic;

    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);
        asyncRunner = new AsyncFacebookRunner(facebook);
        restoreCredentials(facebook);

        String facebookMessage = getIntent().getStringExtra("facebookMessage");
        if (facebookMessage == null) {
            facebookMessage = "Testing App!";
        }
        messageToPost = facebookMessage;
        if (!facebook.isSessionValid()) {
            loginAndPostToWall();
        } else {
            postOnWall("test");
        }
        try {
            String userid = facebook.request("me/friends");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doNotShare(View button) {
        finish();
    }

    public void share(View button) {
        if (!facebook.isSessionValid()) {
            loginAndPostToWall();
        } else {
            postOnWall("test");
        }
    }

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

    protected void postToWall(String userId) {
        String response = null;
        byte[] byteArray = null;
        String id = null;
        try {

            response = facebook.request("me");
            JSONObject me;
            try {
                me = new JSONObject(facebook.request("me"));
                id = me.getString("id");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.v("response", "response" + id);
            User_pic = getUserPic(id);
            // Bitmap bmp = intent.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            User_pic.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byteArray = stream.toByteArray();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Bundle params = new Bundle();
        params.putByteArray("message", userId.getBytes());
        params.putByteArray("caption",
                "{*actor*} just posted a secret message.".getBytes());
        params.putByteArray("description",
                "A secret message is waiting for you.  Click the link to decode it."
                        .getBytes());
        params.putByteArray("name", "A Secret Message For You".getBytes());
        params.putByteArray("picture", byteArray);
        facebook.dialog(this, "stream.publish", params,
                new WallPostDialogListener());
        // asyncRunner.request(((id == null) ? "me" : id) + "/feed", params,
        // "POST", new WallPostDialogListener());
    }

    private Bitmap getUserPic(String id) {
        /**
         * Function loads the users facebook profile pic
         * 
         * @param userID
         */

        String imageURL;
        Bitmap bitmap = null;
        // Log.d(TAG, "Loading Picture");
        imageURL = "http://graph.facebook.com/" + id + "/picture?type=small";
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageURL)
                    .getContent());
            Log.v("get user pic", "get user pic");
        } catch (Exception e) {
            Log.d("TAG", "Loading Picture FAILED");
            e.printStackTrace();
        }
        return bitmap;

    }

    public void postOnWall(String msg) {
        Log.d("Tests", "Testing graph API wall post");
        try {
            String response = facebook.request("me");
            String response1 = facebook.request("me/friends");
            Bundle parameters = new Bundle();
            parameters.putString("message", msg);
            parameters.putString("description", "test test test");
            asyncRunner.request("me/feed", parameters, "POST",
                    new WallPostDialogListener());

            Log.v("respone", "respone" + response1);
            if (response == null || response.equals("")
                    || response.equals("false")) {
                Log.v("Error", "Blank response");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            saveCredentials(facebook);
            if (messageToPost != null) {
                postOnWall(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();
        }
    }

    class WallPostDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                showToast("Message posted to your facebook wall!");
            } else {
                showToast("Wall post cancelled!");
            }
            finish();
        }

        public void onFacebookError(FacebookError e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }

        public void onError(DialogError e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
            finish();
        }

        public void onCancel() {
            showToast("Wall post cancelled!");
            finish();
        }
    }

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

}
4

0 回答 0