在获取facebook数据时,它会引发facebook网络异常和java.net.MalformedURLException:没有协议:me / feed?

时间:2017-08-27 16:44:45

标签: java facebook api facebook-graph-api

Exception in thread "main" com.restfb.exception.FacebookNetworkException: A network error occurred while trying to communicate with Facebook: Facebook request failed (HTTP status code null)
    at 
 com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookC
  lient.java:1024)
    at 
 com.restfb.DefaultFacebookClient.fetchConnectionPage(DefaultFacebookClient.java
    :374)
    at main.main(main.java:26)

    Caused by: java.net.MalformedURLException: no protocol: me/feed
    at java.net.URL.<init>(URL.java:585)
    at java.net.URL.<init>(URL.java:482)
    at java.net.URL.<init>(URL.java:431)
    at com.restfb.DefaultWebRequestor.execute(DefaultWebRequestor.java:365)
    at com.restfb.DefaultWebRequestor.executeGet(DefaultWebRequestor.java:96)
    at 

  com.restfb.DefaultFacebookClient$2.makeRequest(DefaultFacebookClient.java:377)
    at 
 com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookC
   lient.java:1022)
    ... 2 more

   Java Result: 1
   BUILD SUCCESSFUL (total time: 0 seconds)

这是我的错误    当我尝试在我的应用程序上获取user_posts时,请求不会发送    正常。如何解决这个问题?

    /*
    * To change this license header, choose License Headers in Project
    Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author User
    */
    import com.restfb.Connection;
    import com.restfb.DefaultFacebookClient;
    import com.restfb.FacebookClient;
    import com.restfb.types.Post;
    import java.util.*;

    public class main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String accesstoken="EAACEdEose0cBAAXB6i9nmi0VJSZCHI******************";        
        FacebookClient fbclient=new DefaultFacebookClient(accesstoken);
        Connection<Post> result=fbclient.fetchConnectionPage("me/feed", 
                                                               Post.class);        
        int counter=0;

        for(List<Post>page : result)
        {
         for(Post aPost:page)
         {
          System.out.println(aPost.getMessage());
          System.out.print(aPost.getId());
          counter++;        
         }
        }
        System.out.println(" counter "+counter);

    }

   }

在此代码中,访问令牌来自Developer选项。

1 个答案:

答案 0 :(得分:0)

根据fetchConnectionPage(http://restfb.com/javadoc-2/com/restfb/DefaultFacebookClient.html#fetchConnectionPage-java.lang.String-java.lang.Class-)的RestFB API,您需要传入正确的URL。

基本上就在这一行:

Connection<Post> result=fbclient.fetchConnectionPage("me/feed", Post.class);

而不是传入“我/ Feed”,您需要传入一个完整格式的网址,例如“http://facebook.com/me/feed”或该网址应该是什么。

MalformedURLException也暗示了这一点,因为你传递了一个应该是URL的内容;这暗示您没有提供准确的网址。