开发环境是Eclipse+Android SDK+Mongodb+mongo-java-driver
Android的API是8(2.2),最低支持的是1.5,最高支持的是4.4。
Mongodb是用2.6.6
mongo-java-driver是2.13.0-rc2
我是做一个简单的学生资料录入的app。
原理是通过点击 save的button,触发SaveButton_Click,然后SaveButton_Click会把对应的
EditText的内容转换成一个mongodb的文档写入数据库。
编码提示mongo的构造方法已经deprecated,
new Mongo();
new Mongo("localhost");
new Mongo("localhost", 27017);
new Mongo(new ServerAddress("localhost"));
上述4种debug软件会有提示,在模拟器运行也会报错。如何才能解决呢?
MainActivity.java的代码:
package com.example.testing;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.mongodb.*;
public class MainActivity extends ActionBarActivity {
Button button;
int count=0;
String studentname;
String studentnum;
String studentage;
String studentsexual;
Mongo conn;
DB db;
DBCollection coll;
BasicDBObject doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.saveButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("deprecation")
public void SaveButton_Click(View view)
{
try{
conn = new Mongo("localhost");
}catch (Exception e){
throw new RuntimeException(e);
};
DB db = conn.getDB("grade");
DBCollection coll = db.getCollection("Student");
BasicDBObject doc = new BasicDBObject();
doc.put("name", R.id.nameEditText);
doc.put("num",R.id.nameEditText);
doc.put("age",R.id.ageEditText);
doc.put("sexual",R.id.sexualEditText);
coll.setWriteConcern(WriteConcern.SAFE);
}
}