将图形对象数据设置为文本视图

时间:2016-12-08 05:35:51

标签: android facebook

我创建了一个项目。我得到图形对象。现在我想将这些数据设置为textView.I使用facebook sdk v4 +。我的代码在这里。

    import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookRequestError;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

/**
 * Created by Poonam on 12/06/16.
 */
public class Galleryfullimage extends AppCompatActivity {
    CallbackManager callbackManager;
    String username1;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        facebookSDKInitialize();
        setContentView(R.layout.mainscreen);
        tv= (TextView) findViewById(R.id.tv);
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
       // loginButton.setReadPermissions("email");
        getLoginDetails(loginButton);
    }

    protected void facebookSDKInitialize() {
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
    }

    protected void getLoginDetails(LoginButton login_button){
        // Callback registration<br />
        LoginManager.getInstance().logInWithReadPermissions(
                this,
                Arrays.asList("user_friends", "public_profile"));

        login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult login_result) {
                setFacebookData(login_result);
                Intent intent=new Intent(getApplicationContext(),NextClass.class);
                startActivity(intent);

            }
            @Override
            public void onCancel() {
                // code for cancellation<br />
            }
            @Override
            public void onError(FacebookException exception) {
                //  code to handle error<br />
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
        Log.e("data", data.toString());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will<br />
        // automatically handle clicks on the Home/Up button, so long<br />
        // as you specify a parent activity in AndroidManifest.xml.</p>
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onResume() {
        super.onResume();
        // Logs 'install' and 'app activate' App Events.<br />
        AppEventsLogger.activateApp(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        AppEventsLogger.deactivateApp(this);
    }

    private void setFacebookData(final LoginResult loginResult)
    {
        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(final JSONObject object, GraphResponse response) {
                        // Application code

                        try {
                            Log.i("Response", response.toString());

                            //String email = object.getString("email");
                            String firstName = object.getString("first_name");
                            String lastName = object.getString("last_name");
                            String gender =object.getString("gender");
                            String bday= object.getString("birthday");
                            username1=object.getString("first_name");
                            Profile profile = Profile.getCurrentProfile();
                            String id = profile.getId();
                            String link = profile.getLinkUri().toString();
                            Log.i("Link",link);
                            if (Profile.getCurrentProfile()!=null)
                            {
                                Log.i("Login", "ProfilePic" + Profile.getCurrentProfile().getProfilePictureUri(200, 200));
                            }

                           // Log.i("Login" + "Email", email);
                            Log.i("Login"+ "FirstName", firstName);
                            Log.i("Login" + "LastName", lastName);
                            Log.i("Login" + "Gender", gender);
                            Log.i("Login" + "Bday", bday);

//                            Intent intent=new Intent(getApplicationContext(),NextClass.class);
//                            //intent.putExtra("Firstname", username1);
//                            startActivity(intent);
                           // tv.setText(firstName + " " + lastName + " " + gender + " " + bday);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,first_name,last_name,gender,birthday");
        request.setParameters(parameters);
        request.executeAsync();
    }


}

在此我得到回应。但我无法将该响应转换为字符串。意味着无法获得字符串电子邮件,名字等。

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:0)

我们假设您有一个TextView对象mTextView

看看这里说setText的代码吧!

GraphRequest request = GraphRequest.newMeRequest(
            loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(final JSONObject object, GraphResponse response) {
                    // Application code

                    try {
                        Log.i("Response", response.toString());

                        String email = response.getJSONObject().getString("email");
                        String firstName = response.getJSONObject().getString("first_name");
                        String lastName = response.getJSONObject().getString("last_name");
                        String gender = response.getJSONObject().getString("gender");
                        String bday= response.getJSONObject().getString("birthday");

                        Profile profile = Profile.getCurrentProfile();
                        String id = profile.getId();
                        String link = profile.getLinkUri().toString();
                        Log.i("Link",link);
                        if (Profile.getCurrentProfile()!=null)
                        {
                            Log.i("Login", "ProfilePic" + Profile.getCurrentProfile().getProfilePictureUri(200, 200));
                        }


                        Log.i("Login" + "Email", email);
                        Log.i("Login"+ "FirstName", firstName);
                        Log.i("Login" + "LastName", lastName);
                        Log.i("Login" + "Gender", gender);
                        Log.i("Login" + "Bday", bday);

                        //Textview set here
                        mTextView.setText(email + " " +firstName+ " " + lastName + " " + gender +" " + bday);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

答案 1 :(得分:0)

您已从JSONObject方法获得onCompleted。因此,只需从JSONObject object本身获取所有必需值。

跟着这个。

GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(final JSONObject object, GraphResponse response) {
                // Application code

                try {
                    Log.i("Response", response.toString());

                    String email = object.getString("email");
                    String firstName = object.getString("first_name");
                    String lastName = object.getString("last_name");
                    String gender = object.getString("gender");
                    String bday= object.getString("birthday");

                    Profile profile = Profile.getCurrentProfile();
                    String id = profile.getId();
                    String link = profile.getLinkUri().toString();
                    Log.i("Link",link);
                    if (Profile.getCurrentProfile()!=null)
                    {
                        Log.i("Login", "ProfilePic" + Profile.getCurrentProfile().getProfilePictureUri(200, 200));
                    }

                    Log.i("Login" + "Email", email);
                    Log.i("Login"+ "FirstName", firstName);
                    Log.i("Login" + "LastName", lastName);
                    Log.i("Login" + "Gender", gender);
                    Log.i("Login" + "Bday", bday);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
});