变量"图片"来自json GraphRequest Facebook SDK使用gson

时间:2016-09-05 20:25:35

标签: java android json facebook android-studio

我已登录并处理了accessstoken,也得到了GraphResponse FacebookSDK的回复。 我想要实现的是获取数据"图片"来自json回应。

我怎么可能使用gson解析它?或者我可以实际获得数据结果的Pojo类是什么?

这是来自GraphResponse的我的Json响应。

{
  "albums": {
    "data": [
      {
        "photos": {
          "data": [
            {
              "picture": "https://scontent.xx.fbcdn.net/"
            },
            {
              "picture": "https://scontent.xx.fbcdn.net/"
            },
            {
              "picture": "https://scontent.xx.fbcdn.net/",
              "id": "1020051059"
            }
          ],
          "paging": {
            "cursors": {
              "before": "MTAyMODNzAzNDUZD",
              "after": "MTAy1M5ODkwNTkZD"
            }
          }
        },
        "id": "10200419072"
      },
      {
        "photos": {
          "data": [
            {
              "picture": "https://scontent.xx.fbcdn.net/",
              "id": "10248202764"
            }
          ],
          "paging": {
            "cursors": {
              "before": "MTAyMDNzQUyMDI3NjQZD",
              "after": "AyMDU5NzjUyMDI3NjQZD"
            }
          }
        },
        "id": "1020597122762"
      }
    ],
    "paging": {
      "cursors": {
        "before": "MTAyMDA1QE1MDkwNzIZD",
        "after": "MTAyMDNzQ4UxMjI3NjIZD"
      },
      "next": "https://scontent.xx.net/"
    }
  },
  "id": "102088470922"
}

这是我的MainActivity.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        info = (TextView) findViewById(R.id.info);
        loginButton = (LoginButton) findViewById(R.id.login_button);

        initLogin();
    }

    private void initLogin(){
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        // App code

                        USER_TOKEN = loginResult.getAccessToken().getToken();
                        info.setText("Access Token " + USER_TOKEN);
                        initGraph();
                    }

                    @Override
                    public void onCancel() {
                        // App code
                        info.setText("Login attempt canceled.");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        info.setText("Login attempt failed.");
                    }
                });
    }

    private void initGraph() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Insert your code here
                        JSONObject jsonObject = response.getJSONObject();

                        try {
                            Log.i(TAG,jsonObject.getString("albums"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.i(TAG, "-------------------------");
                    }
                });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "albums.limit(2){photos{picture}}");
        request.setParameters(parameters);
        request.executeAsync();

    }

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

就像我说的那样,我已成功获得响应,但我正在努力解决如何解析它(如果可能的话,使用gson)。或者,是否可以使用像#34; Retrofit或OKHTTP这样的Http客户端来捕获&#34; GraphResponse响应&#34;。

到目前为止,我已经能够使用Repofit for Api Call电影数据库进行GET方法,就像基于此url

的TMDB一样

1 个答案:

答案 0 :(得分:0)

我成功获得专辑封面专辑中的总照片以及专辑名称..请访问我的github