Java 如何使用从GraphJSONObjectCallback';s一次完成法

Java 如何使用从GraphJSONObjectCallback';s一次完成法,java,android,facebook-graph-api,Java,Android,Facebook Graph Api,我试图在我的android应用程序中集成通过facebook登录,我想要的是当用户成功登录时,我得到的响应应该自动设置为我在视图中创建的EditText。我通过GraphRequest.GraphJSONObjectCallback()中的onCompleted(如姓名、电子邮件、生日)得到了回复。现在我想将这些值设置到我的EditText字段: GraphRequest request = GraphRequest.newMeRequest( l

我试图在我的android应用程序中集成通过facebook登录,我想要的是当用户成功登录时,我得到的响应应该自动设置为我在视图中创建的
EditText
。我通过
GraphRequest.GraphJSONObjectCallback()
中的
onCompleted
(如姓名、电子邮件、生日)得到了回复。现在我想将这些值设置到我的
EditText
字段:

GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                Log.v("LoginActivity Response ", response.toString());

                                try {
                                    userName = object.getString("name");
                                    nameBox.setText(userName);
}
我在这里调试了线路

nameBox.setText(userName); 
已成功执行,但我的EditText仍然为空。我也试着在电脑上运行这个

activity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            //completeRegistration();
                                            EditText eddt = (EditText) parentView.findViewById(R.id.name);
                                            eddt.setText(userName);
}
但还是不行

编辑@Vulovic Vukasin 出于某些隐私原因,我无法为您提供完整的片段代码,但我可以为您提供与之相关的所有信息 这是我的观点

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        nameBox = (EditText) parentView.findViewById(R.id.name);
 FBloginButton = (LoginButton) parentView.findViewById(R.id.login_button);

        FBloginButton.setReadPermissions(Arrays.asList(
                "public_profile", "email", "user_birthday", "user_friends"));
        // If using in a fragment
        FBloginButton.setFragment(this);
        // Other app specific specialization
        // Callback registration
        FBloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {
                String s = loginResult.toString();
                // App code
                Profile profile = Profile.getCurrentProfile();

                // Facebook Email address
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                Log.v("LoginActivity Response ", response.toString());

                                try {
                                    userName = object.getString("name");
                                    nameBox.setText(userName);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

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


            }
            @Override
            public void onCancel() {
                String s = "did you just cancel login?";
                // App code
            }

            @Override
            public void onError(FacebookException exception) {
                Toast.makeText(getContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
                exception.printStackTrace();
                // App code
            }
        });

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                // Set the access token using
                // currentAccessToken when it's loaded or set.

            }
        };
    return parentView;
}
public View onCreateView(布局、充气机、视图组容器、,
Bundle savedInstanceState){
nameBox=(EditText)parentView.findViewById(R.id.name);
FBloginButton=(LoginButton)parentView.findViewById(R.id.login_按钮);
FBloginButton.setReadPermissions(Arrays.asList(
“公众档案”、“电子邮件”、“用户生日”、“用户朋友”);
//如果在片段中使用
setFragment(this);
//其他特定于应用程序的专门化
//回调注册
registerCallback(callbackManager,newfacebookcallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
字符串s=loginResult.toString();
//应用程序代码
Profile Profile=Profile.getCurrentProfile();
//Facebook电子邮件地址
GraphRequest请求=GraphRequest.newmereRequest(
loginResult.getAccessToken(),
新建GraphRequest.GraphJSONObjectCallback(){
@凌驾
公共空间未完成(
JSONObject对象,
GraphResponse(反应){
Log.v(“LoginActivity响应”,Response.toString());
试一试{
用户名=object.getString(“名称”);
nameBox.setText(用户名);
}捕获(例外e){
e、 printStackTrace();
}
}
});
Bundle参数=新Bundle();
参数.putString(“字段”、“id、姓名、链接、电子邮件、性别、生日”);
请求。设置参数(参数);
request.executeAsync();
}
@凌驾
公开作废{
String s=“您刚才取消登录了吗?”;
//应用程序代码
}
@凌驾
public void onError(facebook异常){
Toast.makeText(getContext(),exception.getMessage(),Toast.LENGTH_SHORT).show();
异常。printStackTrace();
//应用程序代码
}
});
accessTokenTracker=新的accessTokenTracker(){
@凌驾
更改CurrentAccessToken时受保护的void(
AccessToken oldAccessToken,
AccessToken(当前AccessToken){
//使用设置访问令牌
//当加载或设置currentAccessToken时。
}
};
返回父视图;
}

如果您检查文档中的EditText,您会发现一个setText()方法。它接受一个字符串和一个TextView.BufferType。那么就这样做吧

EditText editText = (EditText)findViewById(R.id.name);
editText.setText(username, TextView.BufferType.EDITABLE);


private void runThread() {

    new Thread() {
        public void run() {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            EditText eddt =(EditText)parentView.findViewById(R.id.name);
                            eddt.setText(userName);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}

您是否确保收到的字段不为空?另外,这可能是因为您创建新runnable的方式。该代码可能不允许在UI上进行实际更改,因此我将以正确的方式更新答案Runnable@djhitenYes它们不为null,我在feilds中获取值,并且editText与我需要该值的位置相同,问题是它执行成功,但没有从代码中给出正确的结果。我可以看出,您并不真正需要可运行的。删除未完成中的try catch。另外,如果我在您的位置,我会尝试在onCompleted中更改或显示一个警报框,看看是否可以对主UI进行任何更改。另外,编写一个名为updateEditBox(passusername,birthday…)的私有函数,并在onCompleted()上调用它,怎么样我尝试调用一个单独的函数,为其提供值,但没有成功。您可能需要显示一个警报,以便我可以确认我可以在UI线程上工作,但我无法删除try-catch块,因为它说“您必须处理org.json.JSONException,从JSONObject访问值时可能会发生此异常”。我所能做的最多是,要将这个完整的json对象发送到一个单独的函数,在这里我可以使用try-catch,但首先让我在oncompleted中使用一个alertbox进行尝试,然后保持try-and-catch,并尝试在主UI上更改一些内容,比如更改按钮文本,这样可以工作,请发布整个活动代码,如果您有片段