2013-06-11 45 views
0

我使用JAVA Android SDK開發Deezer應用程序。 我已經設法使用Deezer方法登錄(出現一個允許用戶使用他的deezer或Facebook帳戶登錄的彈出窗口)。Deezer SDK for Android,如何將access_token提供給DeezerRequest?

但現在我想訪問用戶數據(名稱/ ID ...)供將來使用。我目前正在使用

DeezerRequest request_name = new DeezerRequest("/user/me"); 

但是,該應用程序拋出一個異常,詢問access_token。我有它,但我不知道如何給它的請求(和deezer dev網站是不是真的有用,因爲他認爲你知道該怎麼辦)

我可以發佈更多的代碼,如果它是有用的。 感謝您的幫助。

回答

1

登錄後正確地這是我使用的代碼(來自樣品的Deezer應用程序,是在SDK zip文件採取)

在你的活動:

private RequestListener userRequestListenerHandler = new UserRequestHandler(); 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

.... 

searchUser(); 
} 

public void searchUser(){ 

    AsyncDeezerTask searchAsyncUser = new AsyncDeezerTaskWithDialog(this, deezerConnect, userRequestListenerHandler); 
     DeezerRequest request = new DeezerRequest("user/me"); 
     searchAsyncUser.execute(request); 

} 

public void searchUserFinish(User user) { 
     // TODO Auto-generated method stub 
     String name = user.getFirstname(); 
     Sring lastname = user.getLastname(); 
     //... other user data 
     } 


private class UserRequestHandler implements RequestListener{ 

     @Override 
     public void onComplete(String response, Object arg1) { 
      // TODO Auto-generated method stub 

      try{ 


       User user = new DeezerDataReader<User>(User.class).read(response); 

       searchUserFinish(user); 

      }catch (IllegalStateException e){ 
       handleError(e); 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onDeezerError(DeezerError e, Object arg1) { 
      // TODO Auto-generated method stub 
      handleError(e); 
     } 

     @Override 
     public void onIOException(IOException e, Object arg1) { 
      // TODO Auto-generated method stub 
      handleError(e); 
     } 

     @Override 
     public void onMalformedURLException(MalformedURLException e, 
       Object arg1) { 
      // TODO Auto-generated method stub 
      handleError(e); 

     } 

     @Override 
     public void onOAuthException(OAuthException e, Object arg1) { 
      // TODO Auto-generated method stub 
      handleError(e); 
     } 

    } 

public void handleError(Error e){ 
Toast.maketext(this,e,Toast.Length_Short).show(); 
} 

異步的Deezer TASK

public class AsyncDeezerTaskWithDialog extends AsyncDeezerTask { 
    /** Progress dialog to show user that the request is beeing processed. */ 
    private ProgressDialog progressDialog; 

    /** 
    * Simply creates an Deezer task with a dialog. 
    * @param context the context used to create the dialog into. 
    * @param deezerConnect the DeezerConnect object used to connect to Deezer web services. 
    * @param listener the request listener. 
    */ 
    public AsyncDeezerTaskWithDialog(Context context, DeezerConnect deezerConnect, 
      RequestListener listener) { 
     super(deezerConnect, listener); 
     progressDialog = new ProgressDialog(context); 
     progressDialog.setCancelable(true); 
     progressDialog.setOnCancelListener(new OnCancelHandler()); 
    }//met 

    @Override 
    protected void onPreExecute() { 
     progressDialog.setMessage("Contacting Deezer..."); 
     progressDialog.show(); 
     super.onPreExecute(); 
    }//met 

    @Override 
    public void onPostExecute(String s) { 
     //http://stackoverflow.com/questions/2745061/java-lang-illegalargumentexception-view-not-attached-to-window-manager 
     try { 
      if(progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
      }//if 
     } catch (IllegalArgumentException e) { 
      //can happen sometimes, and nothing to get against it 
     }//catch 
     super.onPostExecute(s); 
    }//met 

    @Override 
    protected void onCancelled() { 
     //http://stackoverflow.com/questions/2745061/java-lang-illegalargumentexception-view-not-attached-to-window-manager 
     try { 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
      }//if 
     } catch (IllegalArgumentException e) { 
      //can happen sometimes, and nothing to get against it 
     }//catch 
     super.onCancelled(); 
    }//met 

    private class OnCancelHandler implements OnCancelListener { 
     @Override 
     public void onCancel(DialogInterface dialog) { 
      cancel(true); 
     }//met 
    }//inner class 
}//class 

的Deezer數據讀取器類

public class DeezerDataReader<T extends Object> { 

    /** Class to pass to the Gson parser to create POJOs. */ 
    private Class<T> clazz = null; 

    /** Creates a reader. 
    * @param clazz class to pass to the Gson parser to create POJOs. 
    * */ 
    public DeezerDataReader(Class<T> clazz) { 
     if(clazz == null){ 
      throw new IllegalArgumentException("Clazz can't be null."); 
     }//if 
     this.clazz = clazz; 
    }//cons 

    /** 
    * DAO method to read (deserialize) data from a json string. 
    * @param json the json string to deserialize. 
    * @return a list of typed data from Deezer. The list can't be null, but may be empty. 
    * @throws IllegalStateException if the parser encounters an error in json format. 
    */ 
    public T read(String json) throws IllegalStateException { 
     Gson gson = new Gson(); 
     JsonObject object = new JsonParser().parse(json).getAsJsonObject(); 
     return gson.fromJson(object, clazz); 
    }//met 

}//met 
+0

NOP,還是同樣的問題:/ 十月6日至12日:33:13.710:W/userRequesthandler/onOAuthException(4558):com.deezer.sdk.OAuthException:msg =必須使用活動訪問令牌來查詢有關當前用戶的信息,type =,code = 0 –

+0

我想我有向deezerConnect添加一些東西(通過捆綁或其他),但我不知道什麼和如何^^' –

1

大,有人幫我訪問令牌鏈接到deezerConnect:

/** DeezerConnect object used for auhtentification or request. */ 
private DeezerConnect deezerConnect = new DeezerConnectImpl(APP_ID); 
//---- 
deezerConnect.setAccessToken(getApplicationContext(), access_token); 

的訪問令牌得到感謝

deezerConnect.authorize(MainActivity.this, PERMISSIONS, 
      new LoginDialogHandler()); 

//---- 
private class LoginDialogHandler implements DialogListener { 
    @Override 
    public void onComplete(final Bundle values) { 

     MainActivity.access_token = values.getString("access_token"); 
    } 
//---- 
} 
+1

我們可以在沒有用戶交互的情況下自動登錄嗎? – Dimitri