1

我的应用程序旨在允许用户将视频上传到他们的 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

公共类 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();

}

}

原木猫是:

致命异常:线程 1044 java.lang.NullPointerException

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

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

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

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

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

4

2 回答 2

0

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

在https://developers.facebook.com/blog/post/493/有一个很好的解释它是如何工作的,你可以在How can I make a multipart/form-data找到更多关于从 Java 上传为 multipart/formdata使用Java发布请求?

于 2013-07-26T12:47:58.630 回答
0

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

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

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

于 2013-07-26T20:37:00.407 回答