在这里, initFacebook()
您可以登录并执行您的功能,在这里,我正在获取用户的朋友信息。
private void initFacebook() { try { if (APP_ID == null) { Util.showalert(this,"Warning","Facebook Applicaton ID must be "+ "specified before running this example: see Example.java"); } mFacebook = new Facebook(); mAsyncRunner = new AsyncFacebookRunner(mFacebook); mFacebook.authorize(FacebookList.this, APP_ID, new String[] {"email", "read_stream", "user_hometown", "user_location","friends_about_me", "friends_hometown", "friends_location","user_relationships", "friends_relationship_details","friends_birthday", "friends_education_history","friends_website" }, new DialogListener() { public void onComplete(Bundle values) { getHTTPConnection(); } public void onFacebookError(FacebookError error) { Log.i("public void onFacebookError(FacebookError error)....","...."); } public void onError(DialogError e) { Log.i("public void onError(DialogError e)....", "...."); CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(FacebookList.this, R.style.CustomDialogTheme, Utils.FACEBOOK_CONNECTION_ERROR); dialog.show(); } public void onCancel() { Log.i("public void onCancel()....", "...."); } }); SessionStore.restore(mFacebook, this); SessionEvents.addAuthListener(new SampleAuthListener()); SessionEvents.addLogoutListener(new SampleLogoutListener()); } catch (Exception e) { e.printStackTrace(); }}在中 getHTTPConnection()
,继续进行连接和发送字段,这是我们需要的关于用户朋友的条件,因为在这里我们可以看到传递的字段是
fields=id,first_name,last_name,location,picture朋友。在这里,您可以根据应用程序的要求更改此字段。
private void getHTTPConnection() { try { mAccessToken = mFacebook.getAccessToken(); HttpClient httpclient = new DefaultHttpClient(); String result = null; HttpGet httpget = new HttpGet("https://graph.facebook.com/me/friends?access_token="+ mAccessToken + "&fields=id,first_name,last_name,location,picture"); HttpResponse response; response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); parseJSON(result); } } catch (Exception e) { e.printStackTrace(); }}现在,在成功连接到 Facebook之后 ,我们正在获取
JSON数据并进一步对其进行解析。
private void parseJSON(String data1) throws Exception,NullPointerException, JSonException { try { JSonObject jObj = new JSonObject(data1); JSonArray jObjArr = jObj.optJSonArray("data"); int lon = jObjArr.length(); for (int i = 0; i < lon; i++) { JSonObject tmp = jObjArr.optJSonObject(i); String temp_image = tmp.getString("picture"); String temp_fname = tmp.getString("first_name"); String temp_lname = tmp.getString("last_name"); String temp_loc = null; JSonObject loc = tmp.getJSonObject("location"); temp_loc = loc.getString("name"); } } catch (Exception e) { Log.i("Exception1 is Here>> ", e.toString()); e.printStackTrace(); }}假定您已经在应用程序中添加了一个 facebook jar,并且可以通过调用 initFacebook()
该
onCreate()活动来继续执行此代码。



