gpt4 book ai didi

java - Android facebook sdk 使用graph api

转载 作者:行者123 更新时间:2023-11-30 02:08:46 26 4
gpt4 key购买 nike

我在网上阅读了很多指南,但我还不明白如何在我的 android 应用程序中使用 Graph API。

我有这个 fragment ,其中我有一个 Facebook 登录。登录有效,但我不知道如何获取 Facebook 用户和 ID

这是我的整个 fragment :

public class SampleFragment extends Fragment {

private static final String ARG_POSITION = "position";
private WebView myWebView;
private String LOG_TAG = "AndroidWebViewActivity";

private int position;


public static SampleFragment newInstance(int position) {
SampleFragment f = new SampleFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.i("**********", String.valueOf(object));
}
};

position = getArguments().getInt(ARG_POSITION);
View rootView = inflater.inflate(page, container, false);
FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fabButton);
final ProgressBarCircular progressBarCircular = (ProgressBarCircular) rootView.findViewById(R.id.progress);



final WebView webView = (WebView) rootView.findViewById(R.id.webView);



webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowContentAccess(true);

webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAppCacheEnabled(true);


webView.getSettings().setSupportZoom(false);
webView.setInitialScale(0);

LoginButton loginButton = (LoginButton) rootView.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
// If using in a fragment
loginButton.setFragment(this);
// Other app specific specialization

CallbackManager callbackManager = CallbackManager.Factory.create();


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {



}
});

Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();

Log.i("LOGIN", "***************************");
Log.i("LOGIN", String.valueOf(loginResult));





}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException exception) {

}

});





switch (position) {
case 0:



Bundle webViewBundle1 = null;

if (webViewBundle1 == null) {
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
webView.loadUrl("file:///android_asset/eventiresponsive.html");
}
});
webView.loadUrl("file:///android_asset/eventiresponsive.html");

webView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
progressBarCircular.setVisibility(View.GONE);
}
});
webViewBundle1 = new Bundle();
webView.saveState(webViewBundle1);
}
else {
webView.restoreState(webViewBundle1);
}

loginButton.setVisibility(View.GONE);


break;
case 1:
Bundle webViewBundle2 = null;

fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
webView.loadUrl("file:///android_asset/fotostream.html");
}
});


loginButton.setVisibility(View.GONE);

if (webViewBundle2 == null) {
webView.loadUrl("file:///android_asset/fotostream.html");

webView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
progressBarCircular.setVisibility(View.GONE);
}
});
webViewBundle2 = new Bundle();
webView.saveState(webViewBundle2);

}
else {
webView.restoreState(webViewBundle2);
}


break;
case 2:
Bundle webViewBundle3 = null;

fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
webView.loadUrl("file:///android_asset/visualizzaordini.html");
}
});


loginButton.setVisibility(View.GONE);

if (webViewBundle3 == null) {
webView.loadUrl("file:///android_asset/visualizzaordini.html");

webView.setWebViewClient(new WebViewClient() {

public void onPageFinished(WebView view, String url) {
progressBarCircular.setVisibility(View.GONE);
}
});
webViewBundle3 = new Bundle();
webView.saveState(webViewBundle3);

}
else {
webView.restoreState(webViewBundle3);
}


break;

case 3:
fab.setVisibility(View.GONE);
loginButton.setVisibility(View.VISIBLE);
break;
}

return rootView;
}
@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
CallbackManager callbackManager = CallbackManager.Factory.create();
callbackManager.onActivityResult(requestCode, resultCode, data);
Log.i("LOGIN", "***************************");
Log.i("LOGIN", String.valueOf(data));
}


}

最佳答案

获得访问 token 后,您应该可以调用 https://graph.facebook.com/v2.2/me?access_token=YOUR_ACCESS_TOKEN_HERE

关于java - Android facebook sdk 使用graph api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com