2017-04-26 55 views
0

因此Azure的吐爲我插入到一個活動下面的代碼(Android的工作室是我使用的是什麼)檢查Azure的連接數據庫的onClick進行登錄

以下行添加到包含的.java文件的頂部你的發射活動:

import com.microsoft.windowsazure.mobileservices.*; 

您的活動中,添加一個私有變量

private MobileServiceClient mClient; 

添加以下代碼活動的onCreate方法:

mClient = new MobileServiceClient("https://pbbingo.azurewebsites.net", this); 

到項目::

public class ToDoItem{ public String id; public String Text;} 

添加一個樣本物品類在您定義移動客戶端相同的活動,添加以下代碼:

ToDoItem item = new ToDoItem(); 
item.Text = "Don't text and drive"; 
mClient.getTable(ToDoItem.class).insert(item, new TableOperationCallback<item>(){ 
public void onCompleted(ToDoItem entity, Exception exception, ServiceFilter response) 
{ 
if(exception == null){ 
//Insert Succeeded 
} else { 
//Insert Failed 
} 
}}); 

我的目標是創建一個登錄頁面。我明白,上面提到的可能更多地考慮了ToList。我只想今天獲得正確的語法。我想這個問題是我基本的班級結構。我在創建時創建了一個OnClick Listener,它從我的佈局中的按鈕獲取ID。我不需要檢查數據庫中的任何內容,直到按鈕被實際單擊以登錄或註冊。

public class LoginClass extends AppCompatActivity{ 
     public void onCreate(Bundle savedInstanceState) { 
      setContentView(R.layout.MyLoginLayout); 
      MobileServiceClient mClient = null; 

      try { 
       mClient = new MobileServiceClient ("myAzureWebsite", "AzureKey", this); 
} catch (MalformedURLException e) { 
e.printStackTrace(); 
} 

Button Attempt = (Button) findViewById (R.id.mySubmitButton); 
final MobileServiceClient finalMClient = mClient; // finalized so I can use it later. 

Attempt.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick (View v) { 
     final View thisView = v; 
     final MyToDoItemClass item = new MyToDoItemClass(); 

In MyToDoItemClass I have two variables (Both String) Just left over from 
the example of a ToDoList (they are String ID and String Text) 

     item.Text = "Filler"; 
     item.ID = "Fill"; 
finalMClient.getTable(MyToDoItemClass.class).insert(new Table OperationCallback<item>() { //<--- I'm getting an error that the variable, item 
is from an unknown class... 

public void onCompleted (Item entity, Exception exception, ServiceFilterResponse response){ 
if(exception == null) { 
Intent i = new Intent (LoginClass.this, MainActivity.class); 
startActivity(i); 
}else{ 
Toast.makeText(thisView.getContext(), "Failed", Toast.LENGTH_LONG).show(); 
}} 
}); 
} 
}); 
}} 

問題是與該TableOperationCallback是說,從MyToDoItemClass類項目是從一個未知的類。

回答

1

代碼中有很多問題,如下所示。

  1. 據的Javadoc MobileServiceClient類,沒有一種方法insert(TableOperationCallback<E> callback),所以代碼finalMClient.getTable(MyToDoItemClass.class).insert(new Table OperationCallback<item>() {...}是無效的。

  2. 的仿製藥ETable OperationCallback<E>意味着你需要寫的,而不是一個E POJO類名,而不是一個對象變量名稱,比如item,所以正確的代碼應該是new Table OperationCallback<MyToDoItemClass>,請參閱the Oracle tutorial for Generics瞭解更多的細節。

  3. 下圖顯示了MobileServiceClient類的所有方法insert。方法名下的粗體字Deprecated意味着您不應該將它用於在新項目上進行開發,它只適用於新版Java SDK的舊項目。

enter image description here

請按照官方tutorial來開發你的應用程序。任何問題,請隨時讓我知道。

+0

非常感謝。我認爲提供的方法我們是自給自足的(只要包含導入和正確的lib文件夾),但我現在將它們正確地放在一起。 –