2012-05-19 53 views
-1

我試着從facebook.com收到朋友的請求,但我得到異常Android的Facebook的SDK我/ friendrequests

W/System.err(1139): com.facebook.android.FacebookError: Invalid OAuth access token. W/System.err(1139): at com.facebook.android.Util.parseJson(Util.java:279) W/System.err(1139): at test.project.facebook.AutFacebookActivity$1$2.onComplete(AutFacebookActivity.java:147) W/System.err(1139):at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)

我的Java代碼:

private void authorize(){ 
    intent = new Intent(AutFacebookActivity.this,FaceListView.class); 
     fb.authorize(this,new DialogListener(){ 

      @Override 
      public void onComplete(Bundle values){ 
       Toast.makeText(AutFacebookActivity.this,"Вы вошли",Toast.LENGTH_LONG).show(); 
       asyncRanFace.request("me/friends",new RequestListener(){ 
      public void onComplete(String response, Object state) { 
       try{ 
        JSONObject json = Util.parseJson(response); 
        JSONArray friendsArray = json.getJSONArray("data"); 

        //friendsList = new ArrayList<HashMap>(); 

        String fId,fNm; 
        JSONObject friend; 
        for(int i =0 ; i<friendsArray.length(); i++){ 
         friend = friendsArray.getJSONObject(i); 
         fId = friend.getString("id"); 
         fNm= friend.getString("name"); 
         myFriendsMap.put(fId, fNm); 


        } 
        intent.putExtra("map1",myFriendsMap); 

       }catch(JSONException e){ 
        e.printStackTrace(); 
        } 
       catch(FacebookError e){ 
        e.printStackTrace(); 
       } 
      } 
      @Override 
      public void onFacebookError(FacebookError e, Object state) { 
       // TODO Auto-generated method stub 

      } 
      @Override 
      public void onIOException(IOException e, Object state) { 
       // TODO Auto-generated method stub 

      } 
      @Override 
      public void onFileNotFoundException(FileNotFoundException e, 
        Object state) { 
       // TODO Auto-generated method stub 

      } 
      @Override 
      public void onMalformedURLException(MalformedURLException e, 
        Object state) { 
       // TODO Auto-generated method stub 

      } 


      }); 
       asyncRanFace.request("me/friendrequests?access_token=read_requests",new RequestListener(){ 

          public void onComplete(String res, Object state) { 
           try{ 
            JSONObject jsonRequest = Util.parseJson(res); 
            JSONArray friendsRequestArray = jsonRequest.getJSONArray("data"); 
            JSONObject friendRequest; 
            String fRId,fRNm; 
            for(int i =0 ; i<friendsRequestArray.length(); i++){ 
             friendRequest= friendsRequestArray.getJSONObject(i); 
             fRId = friendRequest.getString("id"); 
             fRNm= friendRequest.getString("name"); 
             myRequestFriendsMap.put(fRId, fRNm); 
             intent.putExtra("map2",myRequestFriendsMap); 
             startActivity(intent); 
            } 

         }catch(JSONException e){ 
          e.printStackTrace(); 
         } 
        catch(FacebookError e){ 
         e.printStackTrace(); 
        } 

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

          } 
          @Override 
          public void onIOException(IOException e, Object state) { 
           // TODO Auto-generated method stub 

          } 
          @Override 
          public void onFileNotFoundException(FileNotFoundException e, 
            Object state) { 
           // TODO Auto-generated method stub 

          } 
          @Override 
          public void onMalformedURLException(MalformedURLException e, 
            Object state) { 
           // TODO Auto-generated method stub 

          } 
         }); 




        } 
      @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 
       Toast.makeText(AutFacebookActivity.this, "Вы вышли", Toast.LENGTH_LONG).show(); 

      } 




     }); 


} 

回答

0

這是你的全部爲Facebook的部分代碼?首先,您需要對Facebook進行Autorize,獲取AccessToken(如果需要,請保存並重用)。

然後,只需:

mAsyncRunner.request("me/friends", new FriendsRequestListener()); 

private class FriendsRequestListener implements RequestListener 
{   
      @Override 
      public void onComplete(String response, Object state) 
      { 
        try 
        { 
          JSONObject json = Util.parseJson(response); 
          JSONArray friends = json.getJSONArray("data");        
        } 
        catch (JSONException e) 
        { 
          Log.e("OnComplete","JSONException"); 
        } 
        catch (FacebookError e) 
        { 
          Log.e("OnComplete","FacebookError"); 
        } 
      } 
      @Override 
      public void onIOException(IOException e, Object state) 
      { 
        Log.e("FriendsRequest","onIOException"); 
      } 
      @Override 
      public void onFileNotFoundException(FileNotFoundException e,Object state) 
      { 
        Log.e("FriendsRequest","onFileNotFoundException"); 
      } 
      @Override 
      public void onMalformedURLException(MalformedURLException e,Object state) 
      { 
        Log.e("FriendsRequest","onMalformedURLException"); 
      } 
      @Override 
      public void onFacebookError(FacebookError e, Object state) 
      { 
        Log.e("FriendsRequest","onFacebookError"); 
      } 
    } 
+0

我怎樣才能獲得的accessToken(如果你需要保存和重新使用它)例如,請?我添加了整個代碼! –

+0

我如何獲得誰加我爲朋友的用戶列表? String token = facebook.getAccessToken(); //獲取訪問令牌 –