2017-04-26 56 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文件夹),但我现在将它们正确地放在一起。 –