user11916264
user11916264

Reputation:

How to get Youtube Api Json Response

I have implement insert rating method in my app . I am searching more than 1 days to get the response . In this page i have check that insert like method and json is giving response . Look here .

But i don't find any way to get the response in my app . How can i solve this ?

@SuppressLint("StaticFieldLeak")
class Insert extends AsyncTask<Object,Object, Object> {

    @Override
    protected Object doInBackground(Object... objects) {



        if (Email!=null){

            mCredential = GoogleAccountCredential.usingOAuth2(
                    getApplicationContext(), Arrays.asList(YouTubeScopes.YOUTUBE))
                    .setBackOff(new ExponentialBackOff());



            mCredential.setSelectedAccountName(Email);

            HttpTransport transport = AndroidHttp.newCompatibleTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            YouTube youtubeService = new YouTube.Builder(
                    transport, jsonFactory, mCredential)
                    .setApplicationName(LikeInsertActivity.this.getResources().getString(R.string.app_name))
                    .build();
            // Define and execute the API request

            try {
                YouTube.Videos.Rate request = youtubeService.videos()
                        .rate(VID, "like");

                request.execute();








                runOnUiThread(new Runnable() {
                    public void run() {


                    }
                });


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

            }

        }

        return null;

    }



}

Upvotes: 0

Views: 677

Answers (2)

second
second

Reputation: 4259

As @mavriksc mentions Rate.execute() returns Void. This is due to the fact that all these object are based on the superclass com.google.api.services.youtube.YouTubeRequest<java.lang.Void>

However instead of the execute method you can use other methods defined by AbstractGoogleClientRequest which is a super class of YouTubeRequest.

For example executeUnparsed returns a com.google.api.client.HttpResponse object.

So obtaining that HttpResponse object and checking the StatusCode vs 204 seems to be the solution you want to have.


Example:

try {
    final YouTube.Videos.Rate request = youtubeService.videos().rate(VID, "like");

    runOnUiThread(new Runnable() {
        public void run() {

            HttpResponse response = request.executeUnparsed();

            // There should be a matching constant for 204 defined somewhere, I haven't found it yet
            if (response.getStatusCode() == 204) {
                //  request successfull
            }
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}

Note:
Android forces developers to do certain (time consuming) things (like NetworkCommunication) in a background task to prevent the UI from blocking.

Upvotes: 1

mavriksc
mavriksc

Reputation: 1132

The return type of Rate.execute() is Void. looking at the HTTP it seems like you get a 204 no content on a good response and an exception otherwise.
https://developers.google.com/youtube/v3/docs/videos/rate

Upvotes: 1

Related Questions