2013-08-16 99 views
0

我使用兩個活動之一是有登錄按鈕,這使得用戶登錄到Facebook和其他有ListView。成功登錄後,列表視圖活動將打開,其中應顯示列表視圖中的朋友姓名及其個人資料圖片。但只有名字出現。任何人都可以告訴我我的代碼中的錯誤在哪裏。朋友個人資料圖片不顯示名單在列表視圖

  public class LoginActivity extends Activity { 

       private static final String[] PERMISSIONS = new String[] { "publish_stream", 
         "publish_checkins", "read_stream", "offline_access", "friends_photos" }; 

       public static final String APP_ID = "**************"; 
       private Facebook facebook = new Facebook(APP_ID); 
       private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
       private ProgressDialog mProgress; 
       private Handler mHandler = new Handler(); 
       private ProgressDialog mSpinner; 
       private Handler mRunOnUi = new Handler(); 
       String FILENAME = "AndroidSSO_data"; 
       private SharedPreferences mPrefs; 

       ArrayList<String> friends; 
       String _error; 
       // private String graph_or_fql; 
       private ListView list; 
       TextView tv; 
       Button loginButton; 
       private UiLifecycleHelper uiHelper; 
       private ContextWrapper uiActivity; 

       @Override 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setSoftInputMode(
          WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
        setContentView(R.layout.login); 

        friends = new ArrayList<String>(); 

        tv = (TextView) LoginActivity.this.findViewById(R.id.textview1); 
        loginButton = (Button) findViewById(R.id.button_login); 

        loginButton.setOnClickListener(new View.OnClickListener() { 

         @SuppressWarnings("deprecation") 
         @Override 
         public void onClick(View v) { 

          if (!facebook.isSessionValid()) { 
           facebook.authorize(LoginActivity.this, PERMISSIONS, 
             new LoginDialogListener()); 
          } 
         } 
        }); 
       } 

       public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        Log.d("FB Demo App", "onActivityResult(): " + requestCode); 
        facebook.authorizeCallback(requestCode, resultCode, data); 
       } 

       private class LoginDialogListener implements DialogListener { 

        public void onComplete(Bundle values) { 
         saveCredentials(facebook); 
         getAlbumsData task = new getAlbumsData(); 
         task.execute(); 
         mHandler.post(new Runnable() { 
          public void run() { 


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

          } 
         }); 

        } 


       private class FriendsRequestListener implements RequestListener { 
        String friendData; 

        // Method runs when request is complete 
        public void onComplete(String response, Object state) { 
         Log.v("", "FriendListRequestONComplete"); 
         // Create a copy of the response so i can be read in the run() method. 
         friendData = response; 
         Log.v("friendData--", "" + friendData); 
         // Create method to run on UI thread 
         LoginActivity.this.runOnUiThread(new Runnable() { 
          @SuppressWarnings("deprecation") 
          public void run() { 
           try { 
            // Parse JSON Data 
            JSONObject json; 
            json = Util.parseJson(friendData); 

            // Get the JSONArry from our response JSONObject 
            JSONArray friendArray = json.getJSONArray("data"); 

            Log.v("friendArray--", "" + friendArray); 

            for (int i = 0; i < friendArray.length(); i++) { 
             JSONObject frnd_obj = friendArray.getJSONObject(i); 
             friends.add(frnd_obj.getString("name")); 

            } 

            Intent ide = new Intent(LoginActivity.this, FrndActivity.class); 

            ide.putStringArrayListExtra("friends", friends); 

            startActivity(ide); 
            finish(); 

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


       @Override 
       public boolean onCreateOptionsMenu(Menu menu) { 
        MenuInflater inflater = getMenuInflater(); 
        inflater.inflate(R.menu.main, menu); 
        final MenuItem item = menu.findItem(R.id.action_settings); 

        return true; 
       } 

      } 


     and 

其爲具有列表視圖中FRND活動是:使用下面的代碼

 public class FrndActivity extends Activity { 
      private Button continueButton; 
      TextView tv; 
      ListView lv; 
      ItemAdapter adapter1; 
      ArrayList<String> friends; 

      private static final String[] PERMISSIONS = new String[] { "read_friendlists" }; 
      public static final String APP_ID = "***********"; 
      private Facebook facebook = new Facebook(APP_ID); 
      private AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
      private Handler mHandler; 
      private ProgressDialog mSpinner; 

      String FILENAME = "AndroidSSO_data"; 
      protected static JSONArray jsonArray; 
      protected String graph_or_fql; 
      private SharedPreferences mPrefs; 
      private Handler mRunOnUi = new Handler(); 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       // TODO Auto-generated method stub 
       mHandler = new Handler(); 
       super.onCreate(savedInstanceState); 
       this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       getWindow().setSoftInputMode(
         WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
       setContentView(R.layout.friendlist_screen); 

       if (android.os.Build.VERSION.SDK_INT > 9) { 
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
          .permitAll().build(); 
        StrictMode.setThreadPolicy(policy); 
       } 

       Intent i = getIntent(); 
       friends = i.getStringArrayListExtra("friends"); 
       Log.v("SizeNext--", "" + friends.size()); 
       lv = (ListView) findViewById(R.id.friendsList); 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
         R.layout.rowlayout, R.id.rowtext_top, friends); 


       mSpinner = new ProgressDialog(lv.getContext()); 
       mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       mSpinner.setMessage("Loading..."); 

       tv = (TextView) findViewById(R.id.friendsText); 

       continueButton = (Button) findViewById(R.id.continueButton); 
       continueButton.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         Intent intent = new Intent(FrndActivity.this, LogoutActivity.class); 
         startActivity(intent); 
         finish(); 

        } 
       }); 
      } 

      class ItemAdapter extends BaseAdapter { 

       private final Bitmap Bitmap = null; 
       final LayoutInflater mInflater; 

       private class ViewHolder { 
        public TextView name; 
        public TextView id; 
        public ImageView pro_image; 

       } 

       public ItemAdapter() { 
        // TODO Auto-generated constructor stub 
        super(); 
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       } 

       // @Override 
       public int getCount() { 
        return friends.size(); 
       } 

       // @Override 
       public Object getItem(int position) { 
        return position; 
       } 

       // @Override 
       public long getItemId(int position) { 
        return position; 
       } 

       @Override 
       public View getView(int position, View convertView, ViewGroup parent) { 
        View view = convertView; 
        final ViewHolder holder; 

        if (convertView == null) { 

         view = mInflater.inflate(R.layout.rowlayout, parent, false); 

         holder = new ViewHolder(); 
         holder.name = (TextView) view.findViewById(R.id.rowtext_top); 
         holder.pro_image = (ImageView) view.findViewById(R.id.profilePicture); 
         holder.id = (TextView) view.findViewById(R.id.id); 
         view.setTag(holder); 
        } 
        else { 
         holder = (ViewHolder) view.getTag(); 
        } 

        String[] data = friends.get(position).split("~~~"); 

        holder.name.setText("" + data[0]); 
        holder.id.setText("" + data[1]); 
        holder.pro_image.setImageBitmap(Bitmap); 

        String imageURL; 
        Bitmap bitmap = null; 
        Log.d("", "Loading Picture"); 
        String graph_Api = "https://graph.facebook.com/me/friends?"; 
        String id = null; 
        imageURL = "http://graph.facebook.com/" + id + "/picture?type=small"; 
        try { 
         Log.v("imageURL--", "" + imageURL); 
         bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageURL) 
           .getContent()); 
        } 
        catch (Exception e) { 
         Log.d("And:", "Loading Picture FAILED"); 
         e.printStackTrace(); 
        } 

        return view; 
       } 

      } 
     } 

    if anybody find the error in my code then please tell.. i donot understand how to fetch profile pics. please do not suggest any other code different from this. i want to know where is the mistake in this code? 

    this is my xml layouts: 
    rowlayout: 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/profilePicture" 
      android:layout_width="60dp" 
      android:layout_height="60dp" /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="15dp" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/rowtext_top" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="name" 
       android:textSize="18sp" /> 

      <TextView 
       android:id="@+id/id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="id" 
       android:textSize="12sp" 
       android:visibility="invisible" /> 
     </LinearLayout> 

    </LinearLayout> 

    friendlist_screen has list view and 
    login has only one button 

回答

0

你獲得FREIND名稱:

frnd_obj.getString("name") 

因此使用下面的代碼用於獲取好友的個人資料圖片網址:

frnd_obj.getJSONObject("picture").getJSONObject("data").getString("url") 
+0

它給誤差在logcat的 爲圖象 No值org.json.JSONObject.get(JSONObject.java:354) W/System.err的(3109):\t在org.json。 JSONObject.getJSONObject(JSONObject.java:573) W/System.err(3109):\t at com.example.demo_project.LoginActivity $ FriendsRequestListener $ 1.run(LoginActivity.java:178) W/System.err(3109) :\t at android.os.Handler.handleCallback(Handler.java:730) – Nidhi

+0

你可以在這裏粘貼錯誤日誌嗎?並粘貼您的Json字符串 –

+0

我已將它粘貼在您的評論之上 – Nidhi

相關問題