我正在尝试将数据写入firebase云。它似乎不起作用。你有什么建议?
$.when
答案 0 :(得分:0)
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import com.firebase.client.Firebase;
public class MainActivity extends AppCompatActivity {
String muruga2,key="U10";
Firebase ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
}
public void additem() {
EditText muruga = (EditText) findViewById(R.id.muruga);
muruga2 = muruga.getText().toString();
ref = new Firebase("https://project-7493703210.firebaseio.com/");
postvalue(key,muruga2 )// key must be unique for every entry
Intent inti = new Intent(MainActivity.this,NextActivity.class);
startActivity(inti);
}
}
private void postvalue(String key, String value) {
Post post = new Post(key,value);
final Map<String, Object> postValues = post.toMapBodyStyle();
ref.child("Name").setValue(postValues).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(RegistrationActivity.this, "Data added successfully", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegistrationActivity.this, "Error in adding user", Toast.LENGTH_SHORT).show();
}
});
}
发布课程
@IgnoreExtraProperties
public class Post {
public String value;
public String key;
public Post() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Post(String key,String val) {
this.value = val;
this.key=key;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put(key, val);
}
@Exclude
public Map<String, Object> toMapBodyStyle() {
HashMap<String, Object> result = new HashMap<>();
result.put(key, value);
return result;
}
}