2

I am using gmusic.api for android as a library project for my android application. For authentication purpose I have copied an AsyncTask named GoogleAuthTask from here.

public class GoogleAuthTask extends AsyncTask<String, Void, Boolean> {

private Activity mActivity;

public GoogleAuthTask(Activity activity) {
    mActivity = activity;
}

@Override
protected Boolean doInBackground(String... params) {
    boolean success = false;
    try {
        String authToken = GoogleAuthUtil.getToken(mActivity, params[0],
                "sj");

        if (!TextUtils.isEmpty(authToken)) {
            GoogleMusicApi.createInstance(mActivity);

            success = GoogleMusicApi.login(mActivity, authToken);

            if (!success)
                GoogleAuthUtil.invalidateToken(mActivity, authToken); 
        }
    } catch (UserRecoverableAuthException e) {
        mActivity.startActivityForResult(e.getIntent(), 1001);
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GoogleAuthException e) {
        e.printStackTrace();
    }

    return success;
}

}

From this AsyncTask there is a call to static login() method on GoogleMusicApi class.

public static final boolean login(Context context, String authToken)
{
    if(!TextUtils.isEmpty(authToken))
    {
        SimpleForm form = new SimpleForm().close();
        GoogleMusicApi.setAuthorizationHeader(authToken);
        mHttpClient.post(context, "https://play.google.com/music/listen?hl=en&u=0", new ByteArrayEntity(form.toString().getBytes()), form.getContentType());
        return true;
    }
    else
        return false;
}

If I go into this method & print response of post() call, it returns null. Same is the case for any call to this post(). For example if I make a call to getAllSongs(), it has post() call which again returns null.

public static final ArrayList<Song> getAllSongs(Context context) throws JSONException
{
    return getSongs(context, "");
}

public static final ArrayList<Song> getSongs(Context context, String continuationToken) throws JSONException
{

    SimpleForm form = new SimpleForm();
    form.addField("json", "{\"continuationToken\":\"" + continuationToken + "\"}");
    form.close();

    String response = mHttpClient.post(context, "https://play.google.com/music/services/loadalltracks?u=0&xt=" + getXtCookieValue(), new ByteArrayEntity(form.toString().getBytes()), form.getContentType());

    JSONObject jsonObject = new JSONObject(response);
    Playlist playlist = new Playlist().fromJsonObject(jsonObject);

    ArrayList<Song> chunkedSongList = new ArrayList<Song>();
    chunkedSongList.addAll(playlist.getPlaylist());

    if(!TextUtils.isEmpty(playlist.getContinuationToken()))
        chunkedSongList.addAll(getSongs(context, playlist.getContinuationToken()));

    return chunkedSongList;
}

Am I missing something here? I didn't find any such exact example using these functions.

Edit: Since response of post() call is null always, I get following stacktrace when calling getAllSongs():

12-04 14:33:36.640: E/AndroidRuntime(29132): FATAL EXCEPTION: AsyncTask #1

12-04 14:33:36.640: E/AndroidRuntime(29132): java.lang.RuntimeException: An error occured while executing doInBackground() 12-04 14:33:36.640: E/AndroidRuntime(29132): at android.os.AsyncTask$3.done(AsyncTask.java:278) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 12-04 14:33:36.640: E/AndroidRuntime(29132): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.lang.Thread.run(Thread.java:856) 12-04 14:33:36.640: E/AndroidRuntime(29132): Caused by: java.lang.NullPointerException 12-04 14:33:36.640: E/AndroidRuntime(29132): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 12-04 14:33:36.640: E/AndroidRuntime(29132): at org.json.JSONTokener.nextValue(JSONTokener.java:94) 12-04 14:33:36.640: E/AndroidRuntime(29132): at org.json.JSONObject.(JSONObject.java:154) 12-04 14:33:36.640: E/AndroidRuntime(29132): at org.json.JSONObject.(JSONObject.java:171) 12-04 14:33:36.640: E/AndroidRuntime(29132): at com.android.gm.api.GoogleMusicApi.getSongs(GoogleMusicApi.java:146) 12-04 14:33:36.640: E/AndroidRuntime(29132): at com.android.gm.api.GoogleMusicApi.getAllSongs(GoogleMusicApi.java:134) 12-04 14:33:36.640: E/AndroidRuntime(29132): at com.example.testgmusic.MainActivity$GoogleAuthTask.doInBackground(MainActivity.java:100) 12-04 14:33:36.640: E/AndroidRuntime(29132): at com.example.testgmusic.MainActivity$GoogleAuthTask.doInBackground(MainActivity.java:1) 12-04 14:33:36.640: E/AndroidRuntime(29132): at android.os.AsyncTask$2.call(AsyncTask.java:264) 12-04 14:33:36.640: E/AndroidRuntime(29132): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 12-04 14:33:36.640: E/AndroidRuntime(29132): ... 5 more

  • Can you show the exact stacktrace please? Thanks – K.C. Dec 04 '13 at 08:27
  • If I add log statement in login() method like following, it prints null response value: String response = mHttpClient.post(context, "https://play.google.com/music/listen?hl=en&u=0", new ByteArrayEntity(form.toString().getBytes()), form.getContentType()); Log.d(TAG, "response = " + response); Same thing happens for next call to getAllSongs(). Stacktrace is added in the question above. –  Dec 04 '13 at 09:11
  • Look at question and response : http://stackoverflow.com/q/2603691/1173560 Maybe it can help you. – K.C. Dec 04 '13 at 09:50
  • This link is related to httpclient, whereas in my case all functions calling post() are part of api library which uses GmHttpClient (extending SyncHttpClient). I don't think I should need to change these library functions, because it must be working for others as I couldn't find any similar question on net. Something must be wrong from my end but unable to find that out. –  Dec 04 '13 at 10:03
  • I'm developing an android app for music recognize. But I can't find a simple document for gmusicapi. How you find it? And where? – roghayeh hosseini Jul 09 '21 at 07:08

1 Answers1

0

Resolved.

It was occurring due to invalid google account being used. Before we can get access to this google music play service I had to first register one gmail account here. Then I could use this new account to access google play music service & gmusic api.