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