Android Play Vimeo Video - java

I want to play my Vimeo videos and I've followed the steps in https://github.com/vimeo/vimeo-networking-java.
I used this method to get the Video object and then load it into a WebView.
VimeoClient.getInstance().fetchNetworkContent(String uri, ModelCallback callback)
However, when I logged the result, it seems to indicate that it fails.
Here are my 2 Java files.
MyVimeoApplication.java
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import com.vimeo.networking.Configuration;
import com.vimeo.networking.Vimeo;
import com.vimeo.networking.VimeoClient;
public class MyVimeoApplication extends Application
{
private static final String SCOPE = "private public interact";
private static final boolean IS_DEBUG_BUILD = false;
// Switch to true to see how access token auth works.
private static final boolean ACCESS_TOKEN_PROVIDED = false;
private static Context mContext;
#Override
public void onCreate()
{
super.onCreate();
mContext = this;
Configuration.Builder configBuilder;
// This check is just as for the example. In practice, you'd use one technique or the other.
if (ACCESS_TOKEN_PROVIDED)
{
configBuilder = getAccessTokenBuilder();
Log.d("ACCESS_TOKEN", "PROVIDED");
}
else
{
configBuilder = getClientIdAndClientSecretBuilder();
Log.d("ACCESS_TOKEN", "NOT PROVIDED");
}
if (IS_DEBUG_BUILD) {
// Disable cert pinning if debugging (so we can intercept packets)
configBuilder.enableCertPinning(false);
configBuilder.setLogLevel(Vimeo.LogLevel.VERBOSE);
}
VimeoClient.initialize(configBuilder.build());
}
public Configuration.Builder getAccessTokenBuilder() {
// The values file is left out of git, so you'll have to provide your own access token
String accessToken = getString(R.string.access_token);
return new Configuration.Builder(accessToken);
}
public Configuration.Builder getClientIdAndClientSecretBuilder() {
// The values file is left out of git, so you'll have to provide your own id and secret
String clientId = getString(R.string.client_id);
String clientSecret = getString(R.string.client_secret);
String codeGrantRedirectUri = getString(R.string.deeplink_redirect_scheme) + "://" +
getString(R.string.deeplink_redirect_host);
Configuration.Builder configBuilder =
new Configuration.Builder(clientId, clientSecret, SCOPE, null,
null);
// configBuilder.setCacheDirectory(this.getCacheDir())
// .setUserAgentString(getUserAgentString(this)).setDebugLogger(new NetworkingLogger())
// // Used for oauth flow
// .setCodeGrantRedirectUri(codeGrantRedirectUri);
return configBuilder;
}
public static Context getAppContext() {
return mContext;
}
public static String getUserAgentString(Context context) {
String packageName = context.getPackageName();
String version = "unknown";
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(packageName, 0);
version = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
System.out.println("Unable to get packageInfo: " + e.getMessage());
}
String deviceManufacturer = Build.MANUFACTURER;
String deviceModel = Build.MODEL;
String deviceBrand = Build.BRAND;
String versionString = Build.VERSION.RELEASE;
String versionSDKString = String.valueOf(Build.VERSION.SDK_INT);
return packageName + " (" + deviceManufacturer + ", " + deviceModel + ", " + deviceBrand +
", " + "Android " + versionString + "/" + versionSDKString + " Version " + version +
")";
}
}
MainActivity.java
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.Toast;
import com.vimeo.networking.VimeoClient;
import com.vimeo.networking.callbacks.AuthCallback;
import com.vimeo.networking.callbacks.ModelCallback;
import com.vimeo.networking.model.Video;
import com.vimeo.networking.model.error.VimeoError;
public class MainActivity extends AppCompatActivity
{
private VimeoClient mApiClient = VimeoClient.getInstance();
private ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("All of your API are belong to us...");
// ---- Client Credentials Auth ----
if (mApiClient.getVimeoAccount().getAccessToken() == null) {
// If there is no access token, fetch one on first app open
authenticateWithClientCredentials();
}
}
// You can't make any requests to the api without an access token. This will get you a basic
// "Client Credentials" gran which will allow you to make requests
private void authenticateWithClientCredentials() {
mProgressDialog.show();
mApiClient.authorizeWithClientCredentialsGrant(new AuthCallback() {
#Override
public void success()
{
Toast.makeText(MainActivity.this, "Client Credentials Authorization Success", Toast.LENGTH_SHORT).show();
mProgressDialog.hide();
mApiClient.fetchNetworkContent("https://vimeo.com/179708540", new ModelCallback<Video>(Video.class)
{
#Override
public void success(Video video)
{
// use the video
Log.d("VIDEO", "SUCCESS");
String html = video.embed != null ? video.embed.html : null;
if(html != null)
{
// html is in the form "<iframe .... ></iframe>"
// load the html, for instance, if using a WebView on Android, you can perform the following:
WebView webview = (WebView) findViewById(R.id.webView); // obtain a handle to your webview
webview.loadData(html, "text/html", "utf-8");
}
}
#Override
public void failure(VimeoError error)
{
Log.d("VIDEO", "FAIL");
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
// voice the error
}
});
}
#Override
public void failure(VimeoError error) {
Toast.makeText(MainActivity.this, "Client Credentials Authorization Failure", Toast.LENGTH_SHORT).show();
mProgressDialog.hide();
}
});
}
}

You can achive this by using external library VimeoExtractor. (my example in kotlin)
https://github.com/ed-george/AndroidVimeoExtractor
1. Step one.
Add lib to gradle.
2. Step Two - get stream url
private fun initializePlayer() {
progressBar.visible() //optional show user that video is loading.
VimeoExtractor.getInstance().fetchVideoWithURL(VIMEO_VIDEO_URL, null, object : OnVimeoExtractionListener {
override fun onSuccess(video: VimeoVideo) {
val videoStream = video.streams["720p"] //get 720p or whatever
videoStream?.let{
playVideo(videoStream)
}
}
override fun onFailure(throwable: Throwable) {
Timber.e("Error durning video stream fetch")
}
})
}
3. Step three - play when ready
fun playVideo(videoStream: String) {
runOnUiThread {
progressBar.gone() //hide progressBar
videoView.setBackgroundResource(R.drawable.splash_backgorund)
videoView.setVideoPath(videoStream)
videoView.requestFocus()
videoView.setOnPreparedListener { mp ->
mp.isLooping = true
videoView.start()
}
}
}

Java Version: I used "Jack the Ripper" answer with little changes:
Step 1:
Used below link to extract video path
https://github.com/ed-george/AndroidVimeoExtractor
Step 2: get Vimeo video path
private void initializePlayer() {
if(getIntent().getStringExtra("vimeo_vid_id")!=null){
vid_id = getIntent().getStringExtra("vimeo_vid_id");
}
VimeoExtractor.getInstance().fetchVideoWithIdentifier(vid_id, null, new OnVimeoExtractionListener() {
#Override
public void onSuccess(VimeoVideo video) {
String hdStream = video.getStreams().get("720p");
System.out.println("VIMEO VIDEO STREAM" + hdStream);
if (hdStream != null) {
playVideo(hdStream);
}
}
#Override
public void onFailure(Throwable throwable) {
}
});
}
Step 3: Play video in VideoView
private void playVideo(final String stream) {
runOnUiThread(new Runnable() {
#Override
public void run() {
final VideoView videoView = findViewById(R.id.vimeo_vv);
final MediaController mediacontroller = new MediaController(VimeoVideoActivity.this);
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
videoView.setBackgroundColor(Color.TRANSPARENT);
Uri video = Uri.parse(stream);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
videoView.requestFocus();
videoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
System.out.println("Video Finish");
finish();
}
});
}
});
}

The problem is that you are trying to use a url and the vimeo-networking library is expecting a uri.
The url you have is https://vimeo.com/179708540 - this is a web url to see a video on the Vimeo site. To use the vimeo-networking library you must provide a uri to the method you are using (in this case fetchNetworkContent); for the video in this question the uri would look like /videos/179708540. The vimeo-networking library will then use this uri to construct a url for connection with the Vimeo API - it will end up looking like https://api.vimeo.com/videos/179708540.

You don't need any library to get vimeo video url
You need to follow just these two steps
Call this url with vimeo video ID to get the JSON Response.
Parse the JSON Response
JSONArray streamArray = new JSONObject(<JSON Response String>)
.getJSONObject("request")
.getJSONObject("files")
.getJSONArray("progressive");
//Get info for each stream available
for (int streamIndex = 0; streamIndex < streamArray.length(); streamIndex++)
{
JSONObject stream = streamArray.getJSONObject(streamIndex);
String url = stream.getString("url");
String quality = stream.getString("quality");
}

I solved the issue by authenticating with simpler authentication
You can authenticate as per your choice for playing the link you can use my method in the link below by
I used Vimeo networking library which is the official library of vimeo
Here is the link to my answer
I able to configure what URI should I use
How to play the video by Video View or Exo Player
So, your solution is you can replace the code of native playback to embed code.
The code for embed is here
https://github.com/vimeo/vimeo-networking-java/tree/master#embed
Now replace the Native play code in my solution below to the embed code
Android Play Vimeo Video // this is the solution

Related

Should I tap NFC tag every time I want to write in it?

I'm an android developer
I have an idea to develop an application that writes on NFC tags data. (Only writing)
I have an Edit Text and a button and when I click on the button I write on the NFC the data written in the field
I searched a lot and tried several codes
The only problem is my NFC tags sticks in the back of the phone and the code i'm using tells me I have to tap/tag the NFC tag to write in it
Is there another way to detect the NFC tag sticked on the back of the phone when clicking the button?
I have two activities main activity that will call methods from my NFCManager
I will share below the NFCManager class
NFCManager class:
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import java.io.ByteArrayOutputStream;
import java.util.Locale;
public class NFCManager {
private Activity activity;
private NfcAdapter nfcAdpt;
public NFCManager(Activity activity) {
this.activity = activity;
}
public void verifyNFC() throws NFCNotSupported, NFCNotEnabled {
nfcAdpt = NfcAdapter.getDefaultAdapter(activity);
if (nfcAdpt == null)
throw new NFCNotSupported();
if (!nfcAdpt.isEnabled())
throw new NFCNotEnabled();
}
public void enableDispatch() {
Intent nfcIntent = new Intent(activity, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, nfcIntent, 0);
IntentFilter[] intentFiltersArray = new IntentFilter[] {};
String[][] techList = new String[][] { { android.nfc.tech.Ndef.class.getName() }, { android.nfc.tech.NdefFormatable.class.getName() } };
nfcAdpt.enableForegroundDispatch(activity, pendingIntent, intentFiltersArray, techList);
}
public void disableDispatch() {
nfcAdpt.disableForegroundDispatch(activity);
}
public static class NFCNotSupported extends Exception {
public NFCNotSupported() {
super();
}
}
public static class NFCNotEnabled extends Exception {
public NFCNotEnabled() {
super();
}
}
public void writeTag(Tag tag, NdefMessage message) {
if (tag != null) {
try {
Ndef ndefTag = Ndef.get(tag);
if (ndefTag == null) {
// Let's try to format the Tag in NDEF
NdefFormatable nForm = NdefFormatable.get(tag);
if (nForm != null) {
nForm.connect();
nForm.format(message);
nForm.close();
}
}
else {
ndefTag.connect();
ndefTag.writeNdefMessage(message);
ndefTag.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public NdefMessage createUriMessage(String content, String type) {
NdefRecord record = NdefRecord.createUri(type + content);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
return msg;
}
public NdefMessage createTextMessage(String content) {
try {
// Get UTF-8 byte
byte[] lang = Locale.getDefault().getLanguage().getBytes("UTF-8");
byte[] text = content.getBytes("UTF-8"); // Content in UTF-8
int langSize = lang.length;
int textLength = text.length;
ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + langSize + textLength);
payload.write((byte) (langSize & 0x1F));
payload.write(lang, 0, langSize);
payload.write(text, 0, textLength);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
return new NdefMessage(new NdefRecord[]{record});
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public NdefMessage createExternalMessage(String content) {
NdefRecord externalRecord = NdefRecord.createExternal("com.survivingwithandroid", "data", content.getBytes());
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { externalRecord });
return ndefMessage;
}
}
Methods from my MainActivity:
#Override
protected void onResume() {
super.onResume();
try {
nfcMger.verifyNFC();
//nfcMger.enableDispatch();
Intent nfcIntent = new Intent(this, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nfcIntent, 0);
IntentFilter[] intentFiltersArray = new IntentFilter[] {};
String[][] techList = new String[][] { { android.nfc.tech.Ndef.class.getName() }, { android.nfc.tech.NdefFormatable.class.getName() } };
NfcAdapter nfcAdpt = NfcAdapter.getDefaultAdapter(this);
nfcAdpt.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
}
catch(NFCManager.NFCNotSupported nfcnsup) {
Snackbar.make(v, "NFC not supported", Snackbar.LENGTH_LONG).show();
}
catch(NFCManager.NFCNotEnabled nfcnEn) {
Snackbar.make(v, "NFC Not enabled", Snackbar.LENGTH_LONG).show();
}
}
#Override
protected void onPause() {
super.onPause();
nfcMger.disableDispatch();
}
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("Nfc", "New intent");
// It is the time to write the tag
currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (message != null) {
nfcMger.writeTag(currentTag, message);
dialog.dismiss();
Snackbar.make(v, "Tag written", Snackbar.LENGTH_LONG).show();
} else {
// Handle intent
}
}
There is another and much better way to get notified that a Tag comes in to range especially if you are writing to a Tag, this is called enableReaderMode but you use case is strange.
I'm not sure why you would want an NFC tag stuck on the back of a phone because then it would act like very slow and small sized permanent storage when a file on the phones memory would be far better.
Remember once the Tag comes in to range you get notified via enableForegroundDispatch or enableReaderMode that a Tag has come in to range and get given a Tag object. As long as that Tag does not go out of range and you have stored the Tag object in the global scope of the activity then you can write to it as many times you like and for as long as you like.
Therefore it should be possible if complicated to do what you wand and write (or read) when even the user click a button.
I've not tested whether a Tag object is usable after your App is put in to the background and brought to the foreground again, but I think it is unlikely because a background App might be closed and closure would definitely invalid the Tag object.
But there are 2 problems with your code.
Really calling connect and write to your Tag should never be done on the UI thread as it is IO blocking and could be cancelled which would cause the Tag to be taken out of range and brought back in to range again. Luckily if you use enableReaderMode then you get notified in a separate thread.
You should only call close on the Tag when you no longer want to write to it, at the moment you are calling close after you have written once to it.
So the following will probably work for you but with the limitation that the Tag has to come in to range the first time after the App has started.
Use enableReaderMode to get notified that the Tag initially comes in to range, Store the Tag object in the global Activity scope and connect to it once in the enableReaderMode callback thread.
The from the UI when the button is press, start a new Thread to write to the tag.
Never call close on the Tag Object.
Note I've not tested this as it is a very strange use case.

Integration of Google Play Services with LibGDX

I have been trying out Google Play Services Multiplayer APK, and have tried out the sample code, "Button Clicker" as shown below.
However, I am not sure how to port this over to LibGDX as currently the code is running based off a normal project without the "core" and "androidlauncher" that libGDX has. Does anyone perhaps have any advise on how should i do it? From what i know, core has a Mainclass with a method called render() which always run in a loop. However I cant think of a way of integrating it.
I have checked out several websites , However, could i ask why cant i just place the whole Buttonclicker project into the core module and call methods from it? Like sign in and sign out.
I only require the functionalities currently implemented in Button Clicker
CODE AS FOLLOWS:
/* Copyright (C) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ryanhello;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesStatusCodes;
import com.google.android.gms.games.GamesActivityResultCodes;
import com.google.android.gms.games.multiplayer.Invitation;
import com.google.android.gms.games.multiplayer.Multiplayer;
import com.google.android.gms.games.multiplayer.OnInvitationReceivedListener;
import com.google.android.gms.games.multiplayer.Participant;
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessage;
import com.google.android.gms.games.multiplayer.realtime.RealTimeMessageReceivedListener;
import com.google.android.gms.games.multiplayer.realtime.Room;
import com.google.android.gms.games.multiplayer.realtime.RoomConfig;
import com.google.android.gms.games.multiplayer.realtime.RoomStatusUpdateListener;
import com.google.android.gms.games.multiplayer.realtime.RoomUpdateListener;
import com.google.android.gms.plus.Plus;
import com.google.example.games.basegameutils.BaseGameUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Button Clicker 2000. A minimalistic game showing the multiplayer features of
* the Google Play game services API. The objective of this game is clicking a
* button. Whoever clicks the button the most times within a 20 second interval
* wins. It's that simple. This game can be played with 2, 3 or 4 players. The
* code is organized in sections in order to make understanding as clear as
* possible. We start with the integration section where we show how the game
* is integrated with the Google Play game services API, then move on to
* game-specific UI and logic.
*
* INSTRUCTIONS: To run this sample, please set up
* a project in the Developer Console. Then, place your app ID on
* res/values/ids.xml. Also, change the package name to the package name you
* used to create the client ID in Developer Console. Make sure you sign the
* APK with the certificate whose fingerprint you entered in Developer Console
* when creating your Client Id.
*
* #author Bruno Oliveira (btco), 2013-04-26
*/
public class MainActivity extends Activity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener, RealTimeMessageReceivedListener,
RoomStatusUpdateListener, RoomUpdateListener, OnInvitationReceivedListener {
/*
* API INTEGRATION SECTION. This section contains the code that integrates
* the game with the Google Play game services API.
*/
final static String TAG = "ButtonClicker2000";
// Request codes for the UIs that we show with startActivityForResult:
final static int RC_SELECT_PLAYERS = 10000;
final static int RC_INVITATION_INBOX = 10001;
final static int RC_WAITING_ROOM = 10002;
// Request code used to invoke sign in user interactions.
private static final int RC_SIGN_IN = 9001;
// Client used to interact with Google APIs.
private GoogleApiClient mGoogleApiClient;
// Are we currently resolving a connection failure?
private boolean mResolvingConnectionFailure = false;
// Has the user clicked the sign-in button?
private boolean mSignInClicked = false;
// Set to true to automatically start the sign in flow when the Activity starts.
// Set to false to require the user to click the button in order to sign in.
private boolean mAutoStartSignInFlow = true;
// Room ID where the currently active game is taking place; null if we're
// not playing.
String mRoomId = null;
// Are we playing in multiplayer mode?
boolean mMultiplayer = false;
// The participants in the currently active game
ArrayList<Participant> mParticipants = null;
// My participant ID in the currently active game
String mMyId = null;
// If non-null, this is the id of the invitation we received via the
// invitation listener
String mIncomingInvitationId = null;
// Message buffer for sending messages
byte[] mMsgBuf = new byte[2];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the Google Api Client with access to Plus and Games
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
// set up a click listener for everything we care about
for (int id : CLICKABLES) {
findViewById(id).setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.button_single_player:
case R.id.button_single_player_2:
// play a single-player game
resetGameVars();
startGame(false);
break;
case R.id.button_sign_in:
// user wants to sign in
// Check to see the developer who's running this sample code read the instructions :-)
// NOTE: this check is here only because this is a sample! Don't include this
// check in your actual production app.
if (!BaseGameUtils.verifySampleSetup(this, R.string.app_id)) {
Log.w(TAG, "*** Warning: setup problems detected. Sign in may not work!");
}
// start the sign-in flow
Log.d(TAG, "Sign-in button clicked");
mSignInClicked = true;
mGoogleApiClient.connect();
break;
case R.id.button_sign_out:
// user wants to sign out
// sign out.
Log.d(TAG, "Sign-out button clicked");
mSignInClicked = false;
Games.signOut(mGoogleApiClient);
mGoogleApiClient.disconnect();
switchToScreen(R.id.screen_sign_in);
break;
case R.id.button_invite_players:
// show list of invitable players
intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 3);
switchToScreen(R.id.screen_wait);
startActivityForResult(intent, RC_SELECT_PLAYERS);
break;
case R.id.button_see_invitations:
// show list of pending invitations
intent = Games.Invitations.getInvitationInboxIntent(mGoogleApiClient);
switchToScreen(R.id.screen_wait);
startActivityForResult(intent, RC_INVITATION_INBOX);
break;
case R.id.button_accept_popup_invitation:
// user wants to accept the invitation shown on the invitation popup
// (the one we got through the OnInvitationReceivedListener).
acceptInviteToRoom(mIncomingInvitationId);
mIncomingInvitationId = null;
break;
case R.id.button_quick_game:
// user wants to play against a random opponent right now
startQuickGame();
break;
case R.id.button_click_me:
// (gameplay) user clicked the "click me" button
scoreOnePoint();
break;
}
}
void startQuickGame() {
// quick-start a game with 1 randomly selected opponent
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 1;
Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
MAX_OPPONENTS, 0);
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build());
}
#Override
public void onActivityResult(int requestCode, int responseCode,
Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
switch (requestCode) {
case RC_SELECT_PLAYERS:
// we got the result from the "select players" UI -- ready to create the room
handleSelectPlayersResult(responseCode, intent);
break;
case RC_INVITATION_INBOX:
// we got the result from the "select invitation" UI (invitation inbox). We're
// ready to accept the selected invitation:
handleInvitationInboxResult(responseCode, intent);
break;
case RC_WAITING_ROOM:
// we got the result from the "waiting room" UI.
if (responseCode == Activity.RESULT_OK) {
// ready to start playing
Log.d(TAG, "Starting game (waiting room returned OK).");
startGame(true);
} else if (responseCode == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
// player indicated that they want to leave the room
leaveRoom();
} else if (responseCode == Activity.RESULT_CANCELED) {
// Dialog was cancelled (user pressed back key, for instance). In our game,
// this means leaving the room too. In more elaborate games, this could mean
// something else (like minimizing the waiting room UI).
leaveRoom();
}
break;
case RC_SIGN_IN:
Log.d(TAG, "onActivityResult with requestCode == RC_SIGN_IN, responseCode="
+ responseCode + ", intent=" + intent);
mSignInClicked = false;
mResolvingConnectionFailure = false;
if (responseCode == RESULT_OK) {
mGoogleApiClient.connect();
} else {
BaseGameUtils.showActivityResultError(this,requestCode,responseCode, R.string.signin_other_error);
}
break;
}
super.onActivityResult(requestCode, responseCode, intent);
}
// Handle the result of the "Select players UI" we launched when the user clicked the
// "Invite friends" button. We react by creating a room with those players.
private void handleSelectPlayersResult(int response, Intent data) {
if (response != Activity.RESULT_OK) {
Log.w(TAG, "*** select players UI cancelled, " + response);
switchToMainScreen();
return;
}
Log.d(TAG, "Select players UI succeeded.");
// get the invitee list
final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
Log.d(TAG, "Invitee count: " + invitees.size());
// get the automatch criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0 || maxAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
Log.d(TAG, "Automatch criteria: " + autoMatchCriteria);
}
// create the room
Log.d(TAG, "Creating room...");
RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
rtmConfigBuilder.addPlayersToInvite(invitees);
rtmConfigBuilder.setMessageReceivedListener(this);
rtmConfigBuilder.setRoomStatusUpdateListener(this);
if (autoMatchCriteria != null) {
rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
}
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
Games.RealTimeMultiplayer.create(mGoogleApiClient, rtmConfigBuilder.build());
Log.d(TAG, "Room created, waiting for it to be ready...");
}
// Handle the result of the invitation inbox UI, where the player can pick an invitation
// to accept. We react by accepting the selected invitation, if any.
private void handleInvitationInboxResult(int response, Intent data) {
if (response != Activity.RESULT_OK) {
Log.w(TAG, "*** invitation inbox UI cancelled, " + response);
switchToMainScreen();
return;
}
Log.d(TAG, "Invitation inbox UI succeeded.");
Invitation inv = data.getExtras().getParcelable(Multiplayer.EXTRA_INVITATION);
// accept invitation
acceptInviteToRoom(inv.getInvitationId());
}
// Accept the given invitation.
void acceptInviteToRoom(String invId) {
// accept the invitation
Log.d(TAG, "Accepting invitation: " + invId);
RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(this);
roomConfigBuilder.setInvitationIdToAccept(invId)
.setMessageReceivedListener(this)
.setRoomStatusUpdateListener(this);
switchToScreen(R.id.screen_wait);
keepScreenOn();
resetGameVars();
Games.RealTimeMultiplayer.join(mGoogleApiClient, roomConfigBuilder.build());
}
// Activity is going to the background. We have to leave the current room.
#Override
public void onStop() {
Log.d(TAG, "**** got onStop");
// if we're in a room, leave it.
leaveRoom();
// stop trying to keep the screen on
stopKeepingScreenOn();
if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
switchToScreen(R.id.screen_sign_in);
}
else {
switchToScreen(R.id.screen_wait);
}
super.onStop();
}
// Activity just got to the foreground. We switch to the wait screen because we will now
// go through the sign-in flow (remember that, yes, every time the Activity comes back to the
// foreground we go through the sign-in flow -- but if the user is already authenticated,
// this flow simply succeeds and is imperceptible).
#Override
public void onStart() {
switchToScreen(R.id.screen_wait);
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
Log.w(TAG,
"GameHelper: client was already connected on onStart()");
} else {
Log.d(TAG,"Connecting client.");
mGoogleApiClient.connect();
}
super.onStart();
}
// Handle back key to make sure we cleanly leave a game if we are in the middle of one
#Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
if (keyCode == KeyEvent.KEYCODE_BACK && mCurScreen == R.id.screen_game) {
leaveRoom();
return true;
}
return super.onKeyDown(keyCode, e);
}
// Leave the room.
void leaveRoom() {
Log.d(TAG, "Leaving room.");
mSecondsLeft = 0;
stopKeepingScreenOn();
if (mRoomId != null) {
Games.RealTimeMultiplayer.leave(mGoogleApiClient, this, mRoomId);
mRoomId = null;
switchToScreen(R.id.screen_wait);
} else {
switchToMainScreen();
}
}
// Show the waiting room UI to track the progress of other players as they enter the
// room and get connected.
void showWaitingRoom(Room room) {
// minimum number of players required for our game
// For simplicity, we require everyone to join the game before we start it
// (this is signaled by Integer.MAX_VALUE).
final int MIN_PLAYERS = Integer.MAX_VALUE;
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(mGoogleApiClient, room, MIN_PLAYERS);
// show waiting room UI
startActivityForResult(i, RC_WAITING_ROOM);
}
// Called when we get an invitation to play a game. We react by showing that to the user.
#Override
public void onInvitationReceived(Invitation invitation) {
// We got an invitation to play a game! So, store it in
// mIncomingInvitationId
// and show the popup on the screen.
mIncomingInvitationId = invitation.getInvitationId();
((TextView) findViewById(R.id.incoming_invitation_text)).setText(
invitation.getInviter().getDisplayName() + " " +
getString(R.string.is_inviting_you));
switchToScreen(mCurScreen); // This will show the invitation popup
}
#Override
public void onInvitationRemoved(String invitationId) {
if (mIncomingInvitationId.equals(invitationId)&&mIncomingInvitationId!=null) {
mIncomingInvitationId = null;
switchToScreen(mCurScreen); // This will hide the invitation popup
}
}
/*
* CALLBACKS SECTION. This section shows how we implement the several games
* API callbacks.
*/
#Override
public void onConnected(Bundle connectionHint) {
Log.d(TAG, "onConnected() called. Sign in successful!");
Log.d(TAG, "Sign-in succeeded.");
// register listener so we are notified if we receive an invitation to play
// while we are in the game
Games.Invitations.registerInvitationListener(mGoogleApiClient, this);
if (connectionHint != null) {
Log.d(TAG, "onConnected: connection hint provided. Checking for invite.");
Invitation inv = connectionHint
.getParcelable(Multiplayer.EXTRA_INVITATION);
if (inv != null && inv.getInvitationId() != null) {
// retrieve and cache the invitation ID
Log.d(TAG,"onConnected: connection hint has a room invite!");
acceptInviteToRoom(inv.getInvitationId());
return;
}
}
switchToMainScreen();
}
#Override
public void onConnectionSuspended(int i) {
Log.d(TAG, "onConnectionSuspended() called. Trying to reconnect.");
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed() called, result: " + connectionResult);
if (mResolvingConnectionFailure) {
Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving.");
return;
}
if (mSignInClicked || mAutoStartSignInFlow) {
mAutoStartSignInFlow = false;
mSignInClicked = false;
mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient,
connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error));
}
switchToScreen(R.id.screen_sign_in);
}
// Called when we are connected to the room. We're not ready to play yet! (maybe not everybody
// is connected yet).
#Override
public void onConnectedToRoom(Room room) {
Log.d(TAG, "onConnectedToRoom.");
//get participants and my ID:
mParticipants = room.getParticipants();
mMyId = room.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient));
// save room ID if its not initialized in onRoomCreated() so we can leave cleanly before the game starts.
if(mRoomId==null)
mRoomId = room.getRoomId();
// print out the list of participants (for debug purposes)
Log.d(TAG, "Room ID: " + mRoomId);
Log.d(TAG, "My ID " + mMyId);
Log.d(TAG, "<< CONNECTED TO ROOM>>");
}
// Called when we've successfully left the room (this happens a result of voluntarily leaving
// via a call to leaveRoom(). If we get disconnected, we get onDisconnectedFromRoom()).
#Override
public void onLeftRoom(int statusCode, String roomId) {
// we have left the room; return to main screen.
Log.d(TAG, "onLeftRoom, code " + statusCode);
switchToMainScreen();
}
// Called when we get disconnected from the room. We return to the main screen.
#Override
public void onDisconnectedFromRoom(Room room) {
mRoomId = null;
showGameError();
}
// Show error message about game being cancelled and return to main screen.
void showGameError() {
BaseGameUtils.makeSimpleDialog(this, getString(R.string.game_problem));
switchToMainScreen();
}
// Called when room has been created
#Override
public void onRoomCreated(int statusCode, Room room) {
Log.d(TAG, "onRoomCreated(" + statusCode + ", " + room + ")");
if (statusCode != GamesStatusCodes.STATUS_OK) {
Log.e(TAG, "*** Error: onRoomCreated, status " + statusCode);
showGameError();
return;
}
// save room ID so we can leave cleanly before the game starts.
mRoomId = room.getRoomId();
// show the waiting room UI
showWaitingRoom(room);
}
// Called when room is fully connected.
#Override
public void onRoomConnected(int statusCode, Room room) {
Log.d(TAG, "onRoomConnected(" + statusCode + ", " + room + ")");
if (statusCode != GamesStatusCodes.STATUS_OK) {
Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode);
showGameError();
return;
}
updateRoom(room);
}
#Override
public void onJoinedRoom(int statusCode, Room room) {
Log.d(TAG, "onJoinedRoom(" + statusCode + ", " + room + ")");
if (statusCode != GamesStatusCodes.STATUS_OK) {
Log.e(TAG, "*** Error: onRoomConnected, status " + statusCode);
showGameError();
return;
}
// show the waiting room UI
showWaitingRoom(room);
}
// We treat most of the room update callbacks in the same way: we update our list of
// participants and update the display. In a real game we would also have to check if that
// change requires some action like removing the corresponding player avatar from the screen,
// etc.
#Override
public void onPeerDeclined(Room room, List<String> arg1) {
updateRoom(room);
}
#Override
public void onPeerInvitedToRoom(Room room, List<String> arg1) {
updateRoom(room);
}
#Override
public void onP2PDisconnected(String participant) {
}
#Override
public void onP2PConnected(String participant) {
}
#Override
public void onPeerJoined(Room room, List<String> arg1) {
updateRoom(room);
}
#Override
public void onPeerLeft(Room room, List<String> peersWhoLeft) {
updateRoom(room);
}
#Override
public void onRoomAutoMatching(Room room) {
updateRoom(room);
}
#Override
public void onRoomConnecting(Room room) {
updateRoom(room);
}
#Override
public void onPeersConnected(Room room, List<String> peers) {
updateRoom(room);
}
#Override
public void onPeersDisconnected(Room room, List<String> peers) {
updateRoom(room);
}
void updateRoom(Room room) {
if (room != null) {
mParticipants = room.getParticipants();
}
if (mParticipants != null) {
updatePeerScoresDisplay();
}
}
My current interface code
package com.macrohard.game;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.example.games.basegameutils.GameHelper;
import com.macrohard.game.SomeGame;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;
import com.macrohard.game.ActionResolver;
import com.macrohard.game.MainMenu;
public class AndroidLauncher extends AndroidApplication implements ActionResolver {
private GameHelper gameHelper;
private final static int requestCode = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
gameHelper.enableDebugLog(false);
GameHelper.GameHelperListener gameHelperListener = new GameHelper.GameHelperListener()
{
#Override
public void onSignInFailed(){ }
#Override
public void onSignInSucceeded(){ }
};
gameHelper.setup(gameHelperListener);
//initialize(new SomeGame(), config);
initialize(new MainMenu(this), config);
}
//...
#Override
protected void onStart()
{
super.onStart();
gameHelper.onStart(this);
}
#Override
protected void onStop()
{
super.onStop();
gameHelper.onStop();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
gameHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void signIn()
{
try
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
gameHelper.beginUserInitiatedSignIn();
}
});
}
catch (Exception e)
{
Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage() + ".");
}
}
#Override
public void signOut()
{
try
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
gameHelper.signOut();
}
});
}
catch (Exception e)
{
Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage() + ".");
}
}
#Override
public void rateGame()
{
String str = "Your PlayStore Link";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
}
#Override
public void unlockAchievement()
{
}
#Override
public void submitScore(int highScore)
{
}
#Override
public void showAchievement()
{
}
#Override
public void showScore()
{
}
#Override
public boolean isSignedIn()
{
return gameHelper.isSignedIn();
}
}
LibGDX separates platform specific codes into different modules for providing multiplatform by using the same code base (Core project). So you should implement android specific stuff in android project as you show in the code example.
You can not use any class in the package "com.google.android" in your core project. Because Google Play Services depends on android specific resources. You should read the document about platform specific code
https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code
So, you should prepare an interface which defines your methods you want to use and implement separately for each of platforms that you are planning to support in your project.
It seems that we are both trying to implement a real-time multiplayer using the Google Play Games Services with the very specific architecture of any libGDX project. I suggest you to check out this GitHub repository : GarrapuchoFootball. Tell me if it helps you.

How to save audio file from speech synthesizer in android (android.speech.tts)?

I tried to use this code in my android application:
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap();
String textToConvert = "this is a demo for saving an mp4 file";
String destinationFileName = "/sdcard/test.mp4";
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, textToConvert);
mTts.synthesizeToFile(textToConvert, myHashRender, destinationFileName);
but the methods are outdated because some methods have been changed in the api level 21 update. The TextToSpeech.synthesizeToFile() method's parameters were changed and I don't know which arguments to pass through. Also, android studio doesn't recognize what TextToSpeech.Engine is. How should I change the code above so that the entered text (in type String) is converted to an audio file? Also, am I missing anything?
Update:
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import java.io.File;
import java.util.HashMap;
public class TextToSpeechFile
{
private static String fileLocation;
private static String text;
private TextToSpeech mTts;
public TextToSpeechFile(String fLocation, String txt){
fileLocation = fLocation;
text = txt;
}
public static void createFile(){
TextToSpeech mTts;
HashMap<String, String> myHashRender = new HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
mTts.synthesizeToFile(text, myHashRender, fileLocation);
String utteranceId = "some unique id";
File file = new File(fileLocation);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
Bundle params = new Bundle(); // optional can just pass in null
params.putString("Some key", "Some value");
mTts.synthesizeToFile(text, params, file, utteranceId );
// mTts.synthesizeToFile(textToConvert, null, destinationFileName, utteranceId );
} else {
mTts.synthesizeToFile(text, myHashRender, fileLocation);
}
}
}
Now it says that mTts has not been initialized. The problem is, I don't know how to initialize a TextToSpeech object! I don't know what arguments to pass through. The class above is designed so that it can be used by different activities. The class itself is NOT an Activity class.
Thank you to those who try to stick with me. I'm a beginner in android app development, so please bear with me.
Thank you.
First you need to import this import android.speech.tts.TextToSpeech;
You still have to implement the deprecated method for backward compatibility if you are targeting device running Android pre-lollipop version. Use the below conditional method call:
final String utteranceId = "myTestingId";
File destinationFile = new File(getCacheDir(), utteranceId + ".wav");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTts.synthesizeToFile(textToConvert, null, destinationFile, utteranceId);
} else {
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
mTts.synthesizeToFile(textToConvert, param, destinationFile.getPath());
)
mTts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
#Override
public void onUtteranceCompleted(String s) {
if (s.equals(utteranceId)) {
// start playing the audio file defined at myTestingId.wav
}
}
});
Code to instantiate the speech engine
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
Locale locale = new Locale("en_US");
mTts.setLanguage(locale);
// speech engine is ready to rock
} else {
// speech engine initialization fail
}
}
});

Google Cloud Messaging - Check if device is already registered

I just set up GCM in my Android App. But I have the problem that I don't know how to check if the device is already registered. I work with the new google play services library.
The register part looks like this:
#Override
protected String doInBackground(String... arg0) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context_app);
}
regid = gcm.register(SENDER_ID);
msg = "Dvice registered, registration ID=" + regid;
Log.d("111", msg);
sendRegistrationIdToBackend(regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
How can I modify this that it checks if the device is already registered?
Store the registration id in a databade table or shared preference and when app starting..check whether it is null or not
Google has provided very clear documentation with code.You should use following code:
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
registerReceiver(mHandleMessageReceiver,
new IntentFilter(DISPLAY_MESSAGE_ACTION));
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// Automatically registers application on startup.
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
mDisplay.append(getString(R.string.already_registered) + "\n");
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
boolean registered =
ServerUtilities.register(context, regId);
// At this point all attempts to register with the app
// server failed, so we need to unregister the device
// from GCM - the app will try to register again when
// it is restarted. Note that GCM will send an
// unregistered callback upon completion, but
// GCMIntentService.onUnregistered() will ignore it.
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
#Override
protected void onDestroy() {
if (mRegisterTask != null) {
mRegisterTask.cancel(true);
}
unregisterReceiver(mHandleMessageReceiver);
GCMRegistrar.onDestroy(this);
super.onDestroy();
}
private final BroadcastReceiver mHandleMessageReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
mDisplay.append(newMessage + "\n");
}
};
when you get registration Id, Store it in SharedPreferences, for example:
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",MODE_PRIVATE);
SharedPreferences.Editor editor=shp.edit();
editor.putString("RegID",registrationID).commit;
In the next time before you register check the "anyNameYouLike" if it contain field called RegID Like this:
private boolean isRegistered(Context context){
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",PRIVATE_MODE);
return shp.contains("RegID");
}

Getting a Malformed access token "t ​ype":"OAuthException","code":190

I am writing an android application to get the Facebook user albums and photos and display in my Android application.
I have created a Facebook App with APP_ID 281846961912565.
While creating the Facebook instance, I am passing this id as follows
facebook = new Facebook(APP_ID);
Using this instance, I am able to login to my FB account post on messages on facebook wall programatically.
After logging in, I get an access_token.
I'm using the access token to get the album ids using facebook.request("https://graph.facebook.com/me/albums?access_token="+facebook.getAccessToken());
Now I get {"error":{"message":"Malformed access token ACCESSTOKENACCESSTOKEN?access_token=ACCESSTOKENACCESSTOKEN","t‌​ype":"OAuthException","code":190}}
Can any of you please help me resolve this issue and point out what i am doing wrong.
My code is as follows:
private static final String[] PERMISSIONS = new String[] { "publish_stream","user_photos" };
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.facebook_dialog);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
if (facebookMessage == null) {
facebookMessage = "Test wall post";
}
messageToPost = facebookMessage;
}
R.layout.facebook_dialog is the dialog which pops up asking if a message should be shared on facebook or not. If yes the following method is called.
public void share(View button) {
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
getImagesFromUserAlbum();
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
Later when I do a `private void getImagesFromUserAlbum() {
facebook.getAccessToken();
JSONArray albumss = null;
String response = null;
try {
response = facebook.request("me/albums");
// `
I get the error
{"error":{"message":"Malformed access token ACCESSTOKEN?access_token=ACCESSTOKEN","type":"OAuthException","code":190}}
Thanks for your help.
The code above is now the working copy. Thanks to Bartek.
If you look at the Errors page in the documentation you will see that when you get error 190 you should authorise/reauthorise the user.
I suspect that this happened to you because you first logged in, then added the permissions to access the albums to your application BUT did not log out and log back in. Hence, you need to obtain a new access token which will grant the new permissions to your application.
Please check is there &expires in your access token if yes then remove it because it is not part of access_token and try after that.

Categories