Json to string error android

时间:2015-07-08 15:54:08

标签: android json facebook facebook-graph-api sharedpreferences

today when i was debugging my app, i found a problem, this one was the first time that appears.. and when i log in, in my app Facebook answer me for correct permission so, i've got this output in my logcat:

  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕ {Response:  responseCode: 200, graphObject: {"id":"MYID","name":"MYUSERNAME"}, error: null}
  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕ 0
  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕MYID
  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕ 0

These output are wrong, all times i tried it, outputs was:

  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕ MYUSRNAME
  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕MYID
  07-08 17:43:17.303  32674-32674/com.tekinarslan.material.sample I/string﹕ MYEMAIL

This is my code:

LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        Toast.makeText(getApplicationContext(), "Login successful" + loginResult.getAccessToken().toString(), Toast.LENGTH_LONG).show();
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code

                                        //textView.setText(response.toString());

                                        JSONObject j_user = response.getJSONObject();
                                        try {
                                             KEY_ID = j_user.getString("id");
                                             KEY_EMAIL = j_user.getString("email");
                                             KEY_NAME = j_user.getString("name");
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        Log.i("string", response.toString());
                                        Log.i("string", KEY_EMAIL);
                                       Log.i("string", KEY_ID);
                                        Log.i("string", KEY_NAME);
                                       Log.i("SHAREDPREFS",KEY_NAME);
                                       // Log.i("SHAREDPREFS",KEY_EMAIL);
                                        editor.putString("name", KEY_NAME);
                                        editor.putString("email", KEY_EMAIL);
                                        editor.putString("id", KEY_ID);
                                        editor.commit();
                                        System.out.println(editor.commit());
                                    }
                                });


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



                    }

P.s : in my iphone app it works correctly the Facebook login, so i think that it is in my android app the problem. what i can't figure out is the reason it doesn't works, it always worked

ALL MY CODE

public class FbLoginActivity extends ActionBarActivity {

    private CallbackManager callbackManager;
    private TextView textView;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static String KEY_ID = "0";
    public static String KEY_EMAIL = "0";
    public static String KEY_NAME = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.facebook_login);


        FacebookSdk.sdkInitialize(this.getApplicationContext());


        callbackManager = CallbackManager.Factory.create();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code
                        Toast.makeText(getApplicationContext(), "Login successful" + loginResult.getAccessToken().toString(), Toast.LENGTH_LONG).show();
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code

                                        //textView.setText(response.toString());

                                        JSONObject j_user = response.getJSONObject();

                                        try {
                                             KEY_ID = object.getString("id");
                                            KEY_EMAIL = object.getString("email");
                                             KEY_NAME = object.getString("name");

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        Log.i("stringazzo", response.toString());
                                        Log.i("STRING", object.toString());
                                      //  Log.i("string", KEY_EMAIL);
                                      //  Log.i("string", KEY_ID);
                                      //  Log.i("string", KEY_NAME);
                                     //   Log.i("SHAREDPREFS",KEY_NAME);
                                       // Log.i("SHAREDPREFS",KEY_EMAIL);

                                        editor.putString("name", KEY_NAME);
                                        editor.putString("email", KEY_EMAIL);
                                        editor.putString("id", KEY_ID);
                                        editor.commit();
                                        System.out.println(editor.commit());
                                    }
                                });


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



                    }

                    @Override
                    public void onCancel() {
                        Toast.makeText(getApplicationContext(), "Login canceled", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(getApplicationContext(), "Login error"+exception.getMessage().toString(), Toast.LENGTH_SHORT).show();
                    }
                });



    }

    public void GOFB(View view){

        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));

    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }


    public void goToEvents(View view)
    {
        //Toast.makeText(this, KEY_NAME, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(FbLoginActivity.this, SampleActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

在onCompleted()回调中,你有JSONObject。请试试。根据你的response.toString()的日志输出,它看起来无效json。

@Override
public void onCompleted( JSONObject object, GraphResponse response) {
    try {
        user_id = object.getString("id");
        name = object.getString("name");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}