应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式。利用webservice技术,具体来说就是客户端直接调用服务器端的接口。之前从来没接触这玩意儿,网上搜了个初学者入门的demo:Java WebService 简单实例。用myecipse写了服务器端的webservice:
package com.hyan.service;
import javax.jws.WebService;
import javax.xml.ws.Endpoint; import org.bson.BsonDocument;
import org.bson.Document; import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.util.JSON; @WebService
public class ServiceHello { /**
* 供客户端调用的方法
* @param name 传入参数
* @param name 返回结果
*/
public String getValue(String table,String items){
/*连接到mongodb进行curd操作*/
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = MongoDB shell("tagmanager");
DBCollection collection = db.getCollection(table);
DBObject dbObject = (DBObject)JSON.parse(items);
collection.insert(dbObject);
System.out.println("插入成功!!!");
}catch(Exception e){
e.printStackTrace();
return "fail";
}
return "success";
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:9001/Service/ServiceHello?wsdl",new ServiceHello());
System.out.println("service success!");
} }
这里有个小插曲,我本来打算是将安卓应用上的数据取出来,拼成json字符串再传到webservice中的方法中,再插入到mongodb中,结果按照www.runoob.com的mongodb教程发现只能插入Document类型,只能一个key-value一个key-value这样的去添,这样太烦了吧?难道就不能直接插入个json字符串么?我用MongoDB shell插入数据都直接是一条json字符串插入的啊,后来又搜了下,能通过上面的代码实现这个功能,只不过getDB()这个方法过时了,mongodb官方不推荐用这个方法.....,过时就过时吧,我也找不到其它的方法了。如果哪位有其它方法的话请务必告知,小弟谢谢了!!!
下面就是安卓应用端该怎么写了,这里参考了http://www.cnblogs.com/superpang/p/4911422.html篇和http://blog.csdn.net/mfc2003/article/details/17119135这篇博客
写好安卓端后和myecipse的客户端后,测试发现用电脑Myeclipse客户端可以访问webservice但是安卓端却无法通过内网ip地址访问我电脑上的webservice百度了下还是没有解决问题,改变策略,去nat123申请了个二级域名用外网到内网这种形式来让我手机访问到我电脑上的webservice,搞定!
eclise安卓端的代码:
package com.example.tagmanager.util; import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE; import com.example.tagmanager.db.SqliteDBHelper; import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast; /*Android多线程编程,异步消息处理机制,使用AsycTask*/
public class BackUpByAsync extends AsyncTask<Context, Context, Boolean>{ private SqliteDBHelper dbHelper = new SqliteDBHelper(
MyApplication.getContext(), "TagManager.db", null, 1);
private SQLiteDatabase db = dbHelper.getWritableDatabase();
private ProgressDialog progressDialog;
private Boolean flag = true; @Override
protected void onProgressUpdate(Context... values) {
progressDialog=new ProgressDialog(values[0]);
progressDialog.setTitle("提示");
progressDialog.setMessage("数据备份中...");
progressDialog.show();
} @Override
protected Boolean doInBackground(Context... arg0) {
String namespace = "http://service.hyan.com/";
String methodName = "getValue";
String WSDL_URI = "http://100702b5.nat123.cc:38965/Service/ServiceHello?wsdl"; Cursor cursor;
publishProgress(arg0);
/* 备份通讯录 */
cursor = db.query("Contact", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex("name"));
String telephone = cursor.getString(cursor
.getColumnIndex("telephone"));
String contact = "{\"name\":" + "\"" + name + "\"" + ","
+ "\"telephone\":" + "\"" + telephone + "\"}";
Log.d("debug", contact);
SoapObject request = new SoapObject(namespace, methodName);
request.addProperty("arg0", "contact");
request.addProperty("arg1", contact);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapSerializationEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.bodyOut = request;
envelope.dotNet = false;
HttpTransportSE ht = new HttpTransportSE(WSDL_URI); try {
Log.d("debug", "调用webservice中.........");
ht.call(null, envelope);
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
Log.d("debug", result);
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
} while (cursor.moveToNext());
}
cursor.close();
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
if(flag){
Toast.makeText(MyApplication.getContext(), "备份成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MyApplication.getContext(), "备份失败", Toast.LENGTH_SHORT).show();
}
} }
不过还是没搞明白为什么webservice局域网无法访问....