将视频发布到Facebook墙时的空指针异常(android)

时间:2013-07-26 07:05:19

标签: android facebook facebook-graph-api nullreferenceexception facebook-android-sdk

我的应用程序旨在允许用户将视频上传到他们的Facebook墙上。以下代码与许多其他工作代码示例非常相似此代码为我提供了空指针异常:

这是我的主要课程:

公共类MainActivity扩展了Activity {

Session session;
Facebook facebook=new Facebook("367714506687419");
private int mAuthAttempts = 0;
String access_token=null;
long expires=0;
Context context;
String Permissions[]=new String[] {
        "publish_stream", "read_stream" , "video_upload" , "publish_actions" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button logout=(Button) findViewById(R.id.logout);
    context=this;
    Button upload_video=(Button) findViewById(R.id.upload_video); 
    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {


      @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
            @SuppressWarnings("deprecation")
            @Override
            public void onCompleted(GraphUser user, Response response) {



              if (user != null) {

                  TextView welcome = (TextView) findViewById(R.id.welcome);
                welcome.setText("Hello " + user.getName() + "!");


                if (access_token != null) {
                    facebook.setAccessToken(access_token);
                }

                if (expires != 0) {
                    facebook.setAccessExpires(expires);
                }

                 if (!facebook.isSessionValid()) {

                        facebook.authorize(MainActivity.this, Permissions,              new DialogListener(){

                            @Override
                            public void onComplete(Bundle values) {

                                access_token=facebook.getAccessToken();
                                expires=facebook.getAccessExpires();

                            }

                            @Override
                            public void onFacebookError(FacebookError e) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onError(DialogError e) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void onCancel() {
                                // TODO Auto-generated method stub

                            }

                        });

                    }

              }
            }
          });
        }
      }
    });






    upload_video.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View arg0) {
            if(session.isOpened()){
            //String dataPath = "/mnt/sdcard/DCIM/Camera/VID_20130725_023450.mp4";
            String path="/sdcard/DCIM/Camera/1.mp4";
            byte[] data = null;
            String dataPath = "/sdcard/DCIM/Camera/1.mp4";
            String dataMsg = "Your video description here.";
            String dataName = "1.mp4";
            Bundle param;
            facebook = new Facebook("367714506687419");
            AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
            InputStream is = null;
            try {
                is = new FileInputStream(dataPath);
                data = readBytes(is);
                param = new Bundle();
                param.putString(Facebook.TOKEN,  access_token);
                param.putString("message", dataMsg);
                param.putByteArray("video", data);
                //param.putString("filename", dataName);
                mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
            }
            catch (FileNotFoundException e) {
               e.printStackTrace();
            }
            catch (IOException e) {
               e.printStackTrace();
            }

            }
            else{
                Log.d("Please Check ur access tocken", "");
            }
        }
    });

这是fbRequestListener.java

public class fbRequestListener实现RequestListener {

@Override
public void onComplete(String response, Object state) {
    Log.e("response", response);

}

@Override
public void onIOException(IOException e, Object state) {
    Log.e("", "onIOException");
    e.printStackTrace();
}

@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
     Log.e("", "onFileNotFoundException");
        e.printStackTrace();

}

@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
     Log.e("", "onMalformedURLException");
        e.printStackTrace();

}

@Override
public void onFacebookError(FacebookError e, Object state) {
    Log.e("", "onFacebookError");
    e.printStackTrace();

}

}

Log Cat是:

致命异常:Thread-1044  显示java.lang.NullPointerException

at libcore.net.UriCodec.decode(UriCodec.java:149)

在java.net.URLDecoder.decode(URLDecoder.java:45)

at com.facebook.android.Util.openUrl(Util.java:186)

at com.facebook.android.Facebook.requestImpl(Facebook.java:806)

at com.facebook.android.AsyncFacebookRunner $ 2.run(AsyncFacebookRunner.java:**

2 个答案:

答案 0 :(得分:0)

您似乎试图直接将视频推送到图形API(graph.facebook.com),而它需要将graph-video.facebook.com作为multipart / formdata转发

有一个很好的解释,它在https://developers.facebook.com/blog/post/493/如何运作,您可以在How can I make a multipart/form-data POST request using Java?

找到有关从Java上传为multipart / formdata的更多信息

答案 1 :(得分:0)

您在代码中混合了两种不同的范例。请仅使用Session类而不是Facebook类(因为它已被弃用)。

此外,由于您使用的是3.0版本的SDK,因此有一种帮助方法可以为您上传视频。请参阅此处的文档:

https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadVideoRequest(Session,%20File,%20Callback)