2013-03-13 32 views
2

我希望能夠將Twitter時間線添加到我的應用程序中,並允許用戶更新他/她的狀態。問題是我無法弄清楚如何進入登錄屏幕。我試着在網上搜索,但所有的都是過時的,我想爲4.0+版本開發。將Twitter API添加到我的應用程序

這是我到目前爲止:代碼似乎並沒有工作,因爲我用於Android> 4.0的Android應用程序,所以它不會編譯15或更高的SDK。另外這一次我使用的是Twitter4J庫,並且這些文件已經全部更新了。

package com.shirwa.texthero.fragments; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.shirwa.texthero.R; 

public class TwitterTab extends Activity implements OnClickListener { 

    static final String TAG = "MyTwitter"; 

    TwitterTab twitter; 
    SharedPreferences prefs; 

    Button buttonUpdate; 
    Button buttonPrefs; 
    EditText textStatus; 

    @Override 
    public void onClick(View src) { 
     String status = textStatus.getText().toString(); 

     Log.d(TAG, "Clicked on " + status); 

     // Toast 
     Toast.makeText(this, textStatus.getText(), Toast.LENGTH_LONG).show(); 

     // set twitter status 
     twitter.updateStatus(status); 

     // reset status string 
     textStatus.setText(""); 
    } 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // find views by id 
     buttonUpdate = (Button) findViewById(R.id.buttonUpdate); 
     textStatus = (EditText) findViewById(R.id.textStatus); 

     // Add listener 
     buttonUpdate.setOnClickListener(this); 

     // Initialize twitter 
     prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     String username = prefs.getString("username", "n/a"); 
     String password = prefs.getString("password", "n/a"); 
     if (username != null && password != null) { 
      twitter = new Twitter(username, password); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 

    // Called when menu item is selected // 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.menuPrefs: 
      // Launch Prefs activity 
      Intent i = new Intent(TwitterTab.this, Prefs.class); 
      startActivity(i); 
      Log.d(TAG, "MenuPrefs starting Prefs"); 
      Toast.makeText(TwitterTab.this, textStatus.getText(), 
        Toast.LENGTH_LONG).show(); 
      break; 
     } 
     return true; 
    } 
} 
+0

你不知道**什麼**?你已經列出了代碼,但沒有問題本身。爲你的問題增加一點清晰度。什麼在您發佈的代碼中無效。 – 2013-03-13 05:18:51

+0

我做了編輯。對不起,先不清楚。 – 2013-03-13 05:25:19

+0

對不起。我仍然不遵循。你堅持要完成登錄嗎?你是否堅持發佈到Twitter?你卡住取時間表嗎?你堅持讓應用程序在較低的SDK上工作嗎?我絕對失去了。 – 2013-03-13 05:34:27

回答

3

在我開始之前,讓我說開始,我通常不張貼鏈接,只有答案。我可能已經是初學者,但幾乎沒有。除非這篇文章是一個合法有效的文章,如果沒有鏈接就不能完全回答。

要通過你的應用程序開始使用登錄到Twitter的,這裏是一個輝煌的小教程,應該讓你開始:

Android Twitter oAuth Connect Tutorial加上已經登錄後發佈狀態更新

你可以還可以看看GitHub上的一個示例,T4JTwitterLogin。這是使用您的應用程序登錄Twitter併發布狀態更新的另一個示例。

至於獎金的好東西(好吧,其實只是一個)。一旦您對整體開發的Android感到滿意,或者可以開始解密其他開發人員代碼時,這裏是一個完整的功能Android Application for Twitter,它是開源的,可在Google Play store上獲得,並且在其上非常流行。

這是對搜索結果中的鏈接在GitHub上用關鍵字Android和twitter4J:https://github.com/search?q=android+twitter4j&type=Repositories&s=updated

0

你在那裏使用了什麼API?

沒有官方的twitter SDK爲android構建,只是他們的REST API。爲此,我可以親自推薦圖書館「抄寫員」來處理與twitter的整合。它可以處理認證,發佈和REST API中指定的任何其他功能。

https://github.com/fernandezpablo85/scribe-java https://dev.twitter.com/docs/api/1.1

+0

我目前使用API​​ 14(4.0冰淇淋三明治)或更高.. – 2013-03-13 05:26:30

+0

您將需要第三方庫,如Scribe,我鏈接到能夠處理oauth連接到twitter並使用它的REST API。 – 2013-03-13 05:27:27

+0

等等,所以我會將它實現到我的main.java中或爲它創建一個單獨的類? – 2013-03-13 05:32:30

0

首先,你必須驗證您的應用程序,這將需要2祕密密鑰(這將被Twitter您在應用程序提供給你註冊)。

其次,你需要使用Oauth進行身份驗證,你可以谷歌它,(單獨嘗試)。

第三,處理回調並處理所需的響應。 use this for reference

1

我已經開發TWITTER我APP.It工作sucessfully..Here是LINK

您可以開發您的應用。