Android Studio连接到Azure数据库

时间:2018-04-15 19:09:23

标签: java android azure azure-sql-database azure-mobile-services

您好我尝试将项目/类发布到Azure SQL数据库。我遵循微软放在一起的指导方针,但是你知道..

我有这个类:MessageActivity,它会在单击Button时创建一个MessageItem对象。然后,此项应发布到Azure数据库。但它并没有出现在那里&我没有收到错误消息

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.microsoft.windowsazure.mobileservices.*;
import com.microsoft.windowsazure.mobileservices.http.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.table.MobileServiceTable;
import com.microsoft.windowsazure.mobileservices.table.TableOperationCallback;
import java.net.MalformedURLException;
import java.util.concurrent.ExecutionException;

public class MessageActivity extends AppCompatActivity {
//message will be send to database
//    TextView receptorTv = (TextView) findViewById(R.id.receptorTv);
public static FragmentManager fragContainer;

private MobileServiceClient mClient;
private MobileServiceTable mobileServiceTable;

String receptorWho;
EditText firstName;
EditText lastName;
EditText email;
EditText message;
EditText receptorET;
TextView receptorTv;
Button sendToDatabase;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_form);
    Intent intent = getIntent();
    String receptor = intent.getStringExtra("receptor");
    receptorTv = (TextView) findViewById(R.id.receptorTv);
    if (receptor == null) {
        receptor = "Main Office";
    }

    firstName = (EditText) findViewById(R.id.eTFirstName);
    lastName = (EditText) findViewById(R.id.eTLastName);
    email = (EditText) findViewById(R.id.eTEmail);
    message = (EditText) findViewById(R.id.eTMessage);

    sendToDatabase = (Button) findViewById(R.id.sendToDatabase);

    //connect to AzureMobileService

    try {
        mClient = new MobileServiceClient(
                "https://name.azurewebsites.net",
                this
        );
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
  //  createContactTable(view);
}




public void createContactTable(View view){
    Toast.makeText(this, "You clicked the Button", Toast.LENGTH_SHORT);
    MessageItem item = new MessageItem();
    item.setFirstName(firstName.getText().toString());
    item.setLastName(lastName.getText().toString());
    item.setEmailAddress(email.getText().toString());
    item.setReceptor(receptorWho);
    item.setMessage(message.getText().toString());

    mClient.getTable(MessageItem.class).insert(item);

    mClient.getTable(MessageItem.class).insert(item, new TableOperationCallback<MessageItem>() {
        @Override
        public void onCompleted(MessageItem entity, Exception exception, ServiceFilterResponse response) {
            if (exception==null){
                //success
                Toast.makeText(MessageActivity.this, "Successful", Toast.LENGTH_SHORT);
            } else {
                //failed
                Toast.makeText(MessageActivity.this, "Failed", Toast.LENGTH_SHORT);
            }
        }
    });
}
}

我的MessageItem看起来像这样:

public class MessageItem {

@com.google.gson.annotations.SerializedName("ID")
public String id;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}

@com.google.gson.annotations.SerializedName("FIRSTNAME")
public String firstName;
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@com.google.gson.annotations.SerializedName("LASTNAME")
public String lastName;
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

@com.google.gson.annotations.SerializedName("EMAIL")
public String emailAddress;
public String getEmailAddress() {
    return emailAddress;
}
public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
}

@com.google.gson.annotations.SerializedName("RECEPTOR")
public String receptor;
public String getReceptor() {
    return receptor;
}
public void setReceptor(String receptor) {
    this.receptor = receptor;
}

@com.google.gson.annotations.SerializedName("MESSAGE")
public String message;
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}

public MessageItem() {
}

public MessageItem(String id, String firstName, String surName, String emailAddress, String receptor, String message) {
    this.setId(id);
    this.setFirstName(firstName);
    this.setLastName(surName);
    this.setEmailAddress(emailAddress);
    this.setReceptor(receptor);
    this.setMessage(message);
}

@Override
public boolean equals(Object o) {
    return o instanceof MessageItem && ((MessageItem) o).id == id;
}

@Override
public String toString() {
    return getMessage();
}


}

xml中的Button看起来像这样:

 <Button
            android:id="@+id/sendToDatabase"
            android:onClick="createContactTable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="8dp"
            android:text="send message"
            android:textAllCaps="false"
            android:textColor="#000"
            android:textStyle="italic"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/eTMessage"
             />

我希望你能发现我的错误。我在Android.Manifest&amp;中宣布了一个Internet.Permission。 build.gradle中的所有依赖项都在工作 谢谢马克斯:)

1 个答案:

答案 0 :(得分:1)

你必须在类属性上面加上像这样的gson注释:

@com.google.gson.annotations.SerializedName("NAME_OF_ATTRIBUTE")

例如:

@com.google.gson.annotations.SerializedName("id")
public String id;
public String getId() {return id;}
public void setId(String id) {this.id = id;}

@com.google.gson.annotations.SerializedName("firstName")
public String firstName;
public String getFirstName() {return firstName;}
public void setFirstName(String firstName) {this.firstName = firstName;}

文档here