» 首頁 » 討論區 » Android程式設計 »facebook userid 變username 問題

facebook userid 變username 問題

發表人: ooaaa
積分: 158
發表時間: 2011-10-05 20:45:12
如題...我可以得到facebook userid...如何從userid 變成 username ...請問大大們 有參考的語句 or 範例嗎??

我找到
function getName($id)
{
$facebookUrl = "https://graph.facebook.com/".$id;
$str = file_get_contents($facebookUrl);
$result = json_decode($str);
return $result->name;
}


echo getName('1191614041');

但是他會出錯..有方法可改??
發表人: Seachaos
積分: 2432
發表時間: 2011-10-05 22:32:02
您好:
這有可能是兩個地方有問題
1. 你的Facebook API是用https,很多Server沒有設定的話常常在存取上會出問題,改成http應該就ok了
2. Apache Server不支援遠端的File read/write

我有用PHP的Curl改寫了這個Facebook API
或許你可以試看看這個簡單的PHP函數
[sea:javaCode]
function getFacebookUserName($id){
$facebookUrl = "http://graph.facebook.com/".$id;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $facebookUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = json_decode(curl_exec($curl));
curl_close($curl);
return $result->name;
}
[/sea]
發表人: ooaaa
積分: 158
發表時間: 2011-10-05 23:46:47
己解決
發表人: ooaaa
積分: 158
發表時間: 2011-10-24 09:20:33
大大...我應該如何把fb的大頭照放進 我的 adapter的"pic"..我已經有fb的id...只欠如何放進adpater 的 "pic"...


[sea:javaCode]
adapter = new SimpleAdapter(
this,
list,
R.layout.b,
new String[] { "xx","bb","pic"},
new int[] { R.id.c, R.id.e,R.id.d} );
[/sea]


[sea:javaCode]
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);
[/sea]

請大大教學...
發表人: Seachaos
積分: 2432
發表時間: 2011-10-25 13:10:17
如果你要自定Adapter的話應該可以
Facebook用 url to image 的方法沒有問題
發表人: ooaaa
積分: 158
發表時間: 2011-10-25 15:51:17
大大有可參考的範例嗎???
發表人: Seachaos
積分: 2432
發表時間: 2011-10-27 01:05:48
如果你是用Activity的話
要先找到你的ImageView

[sea:javaCode]
imageView = (Image)findViewById(R....
// 然後就可以指派網路上的圖片到ImageView了
URL url = new URL("http://www.myandroid.tw/style/images/logo.png");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream is = con.getInputStream();
imageView.setImageBitmap(BitmapFactory.decodeStream(is));
[/sea]
發表人: ooaaa
積分: 158
發表時間: 2011-10-27 01:08:05
但是我放不進去...


[sea:javaCode]

adapter = new SimpleAdapter(
this,
list,
R.layout.b,
new String[] { "xx","bb","pic"},
new int[] { R.id.c, R.id.e,R.id.d} );
[/sea]
pic的位置 有方法可以放進去嗎 ?
[sea:javaCode]
map.put("name", name);
map.put("comment", COMMENT);
map.put("pic",????);
list.add(map);
[/sea]

???的位置應該要怎弄???
發表人: Seachaos
積分: 2432
發表時間: 2011-10-27 02:02:01
嗯…
你這兩個片斷我看不出大概
因我還是不太清楚你的資料結構
發表人: 訪客
發表時間: 2011-10-27 13:30:14
晚一點我寄檔案給你看@@
發表人: ooaaa
積分: 158
發表時間: 2011-10-29 02:18:04
大大 你有頭緒嗎 ??
發表人: Seachaos
積分: 2432
發表時間: 2011-10-30 15:17:55
ok
我有看到了

以下是我的寫法
因為網路來的圖片是Bitmap,而SimpleAdapter預設沒有辦法使用BitmapImage
所以要定議遇到Bitmap的方法,就要寫個viewBinder
[sea:javaCode]

ArrayList<Map<String, Object>> contents = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
try {
// Get image from net
URL url = new URL(
"http://www.myandroid.tw/style/images/logo.png");
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
InputStream is = con.getInputStream();
map.put("PIC", BitmapFactory.decodeStream(is));
} catch (IOException e) {
map.put("PIC", R.drawable.icon);
Log.e("msg", e.toString());
}

map.put("Name", "MyAndroid");
contents.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,
(List<Map<String, Object>>) contents, R.layout.listitem,
new String[] { "PIC", "Name" }, new int[] { R.id.listitem_pic,
R.id.listitem_title });

// 重寫 ViewBinder 讓 Bitmap可以設定在ImageView上
adapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// 檢查是否是ImageView和Bitamp
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}
});
[/sea]

這是我的listitem的xml
[sea:javaCode]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight">
<ImageView android:id="@+id/listitem_pic"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_alignParentTop="true" android:layout_alignParentBottom="true"
android:src="@drawable/icon" android:adjustViewBounds="true"
android:padding="2dip" />
<TextView android:id="@+id/listitem_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/listitem_pic"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:layout_above="@+id/listitem_content"
android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
android:text="@+id/listitem_title" android:textSize="22px" />
</RelativeLayout>
[/sea]