2014-10-07 309 views
1

我很新的android。從twitter獲取個人資料圖片圖像使用解析推特登錄爲Android

我使用Parse作爲Baas的Twitter身份驗證,據我所知,我只能得到一個com.parse.twitter.Twitter對象而不是twitter4j.Twitter對象。

我試圖下載和Android應用中使用此代碼顯示來自Twitter的個人資料圖片圖像:

String twitterURL = "https://api.twitter.com/1.1/users/profile_banner.json?screen_name=myScreenName"; 

//根據這裏的文檔https://dev.twitter.com/rest/reference/get/users/profile_banner

然後,我想使用Picasso Picasso.with(aView.getContext())。load(twitterURL).resize(100,150).centerCrop()。into(imageView);

不顯示任何...

我也試圖與該代碼:

URL url = new URL(twitterURL); 
InputStream is = (InputStream) url.getContent(); 
Drawable d = Drawable.createFromStream(is, "src name"); 

但得到錯誤FileNotFoundException異常。

非常感謝您的幫助。

+0

你從API調用中獲得什麼?據我所知,你需要使用Twitter進行所有API調用的認證,而且你似乎沒有這樣做。 – michaelcarrano 2014-10-07 21:17:48

+0

感謝您的回答。我使用Twitter帳戶在我的登錄頁面進行身份驗證。在那一刻,我保存在我的Parse數據庫中的用戶的屏幕名稱,以便用以上代碼獲取他的個人資料圖片。當我使用InputStream代碼時,我得到一個FileNotFoundException。隨着畢加索,沒有例外拋出。也許,我在屏幕上認證的事實是不夠的? – 2014-10-08 09:16:23

回答

3

我有這個問題我自己,從來沒有找到一個很好的答案,所以你的代碼讓我看着辦吧!

您正在提取的InputStream實際上是JSON格式(例如{「id」:4646546,「name」:「ian」,「profile_image_url」:「http://pbs.twimg.com/profile_images/1138330557/ian-straw_normal.jpg」})。所以你不能只從它創建一個drawable。 Twitter API文檔給出了一個示例結果:https://dev.twitter.com/rest/reference/get/users/show

如果你想以原始大小獲取圖像,你可以擺脫'_normal'。

使用此profile_image_url,您可以獲取您感興趣的圖像。您可以使用Android Query或Picasso來執行此操作。你也可以做另一個HttpRequest,只要你不在主UI線程上做。

你的代碼變得(我忽略了可讀性try/catch塊):

HttpClient client = new DefaultHttpClient(); 
HttpGet verifyGet = new HttpGet(
    "https://api.twitter.com/1.1/users/show.json?screen_name=" + handle); 
ParseTwitterUtils.getTwitter().signRequest(verifyGet); 
HttpResponse response = client.execute(verifyGet); 
InputStream is = response.getEntity.getContent(); 
JSONObject responseJson = new JSONObject(IOUtils.toString(is)); 
url = responseJson.getString("profile_image_url"); 

final AQuery androidQuery = new AQuery(this); 
//fetch and set the image from internet, cache with file and memory 
androidQuery.id(R.id.image1).image(url); 

注:AQuery爲您提供了更多的選擇。如果需要,您可以直接在位圖中獲取圖像。請參閱:https://code.google.com/p/android-query/wiki/AsyncAPI#Bitmap

+1

它的作品!你是個天才!!非常感謝伊恩。我只需要使用這些代碼並添加apache commons來使用IOUtils。 – 2014-11-04 23:26:09

+0

歡迎傢伙;) – ian 2015-01-09 14:38:24

0

我自己沒有測試過,但我相信它應該指向正確的方向。

Twitter API現在要求您針對每個創建的API請求進行身份驗證。

HttpClient client = new DefaultHttpClient(); 
HttpGet verifyGet = new HttpGet(
    "https://api.twitter.com/1.1/users/profile_banner.json?screen_name=myScreenName"); 
ParseTwitterUtils.getTwitter().signRequest(verifyGet); 
HttpResponse response = client.execute(verifyGet); 
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 

所以我認爲你有兩個問題:

  1. 你似乎並不被作出任何HTTP請求,你剛纔創建的URL。
  2. 你沒有簽署Twitter的要求

https://parse.com/docs/android_guide#twitterusers-requests

+0

感謝您的幫助。我測試了你的解決方案。這似乎是一個非常相互矛盾的方向(鏈接似乎也這樣說)。但仍然沒有圖像顯示。 InpuStream不是空的,但我不能說在裏面有一個設備。所以我嘗試將InputStream轉換爲如下drawable:Drawable d = Drawable.createFromStream(is,「src name」); \t \t \t \t \t imageView.setImageDrawable(d);但對象d仍然是空的。 – 2014-10-09 17:15:08

+1

沒有人有同樣的問題? – 2014-10-16 21:41:20

相關問題