我正在使用两个活动,一个是登录按钮,使用户登录到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 :(得分:0)
使用以下代码获取freind名称:
frnd_obj.getString("name")
因此,请使用以下代码获取朋友个人资料图片网址:
frnd_obj.getJSONObject("picture").getJSONObject("data").getString("url")
答案 1 :(得分:0)
首先检查imageURL是否正确。 如果它是正确的,看看这个,看看你是否能让它适合你。
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/