应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式。利用webservice技术,具体来说就是客户端直接调用服务器端的接口。之前从来没接触这玩意儿,网上搜了个初学者入门的demo:Java WebService 简单实例。用myecipse写了服务器端的webservice:
1 package com.hyan.service; 4 5 import javax.jws.WebService; 6 import javax.xml.ws.Endpoint; 7 8 import org.bson.BsonDocument; 9 import org.bson.Document; 10 11 import com.mongodb.DB; 12 import com.mongodb.DBCollection; 13 import com.mongodb.DBObject; 14 import com.mongodb.MongoClient; 15 import com.mongodb.client.MongoCollection; 16 import com.mongodb.client.MongoDatabase; 17 import com.mongodb.util.JSON; 18 19 @WebService 20 public class ServiceHello { 21 22 /** 23 * 供客户端调用的方法 24 * @param name 传入参数 25 * @param name 返回结果 26 */ 27 public String getValue(String table,String items){ 28 /*连接到mongodb进行curd操作*/ 29 try{ 30 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); 31 DB db = MongoDB shell("tagmanager"); 32 DBCollection collection = db.getCollection(table); 35 DBObject dbObject = (DBObject)JSON.parse(items); 36 collection.insert(dbObject); 38 System.out.println("插入成功!!!"); 45 }catch(Exception e){ 46 e.printStackTrace(); 47 return "fail"; 48 } 49 return "success"; 50 }55 56 public static void main(String[] args) { 57 Endpoint.publish("http://localhost:9001/Service/ServiceHello?wsdl",new ServiceHello()); 58 System.out.println("service success!"); 59 } 60 61 }
这里有个小插曲,我本来打算是将安卓应用上的数据取出来,拼成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安卓端的代码:
1 package com.example.tagmanager.util; 2 3 import org.ksoap2.serialization.SoapObject; 4 import org.ksoap2.serialization.SoapSerializationEnvelope; 5 import org.ksoap2.transport.HttpTransportSE; 6 7 import com.example.tagmanager.db.SqliteDBHelper; 8 9 import android.app.ProgressDialog; 10 import android.content.Context; 11 import android.database.Cursor; 12 import android.database.sqlite.SQLiteDatabase; 13 import android.os.AsyncTask; 14 import android.util.Log; 15 import android.widget.Toast; 16 17 /*Android多线程编程,异步消息处理机制,使用AsycTask*/ 18 public class BackUpByAsync extends AsyncTask<Context, Context, Boolean>{ 19 20 private SqliteDBHelper dbHelper = new SqliteDBHelper( 21 MyApplication.getContext(), "TagManager.db", null, 1); 22 private SQLiteDatabase db = dbHelper.getWritableDatabase(); 23 private ProgressDialog progressDialog; 24 private Boolean flag = true; 25 26 @Override 27 protected void onProgressUpdate(Context... values) { 28 progressDialog=new ProgressDialog(values[0]); 29 progressDialog.setTitle("提示"); 30 progressDialog.setMessage("数据备份中..."); 31 progressDialog.show(); 32 } 33 34 @Override 35 protected Boolean doInBackground(Context... arg0) { 36 String namespace = "http://service.hyan.com/"; 37 String methodName = "getValue"; 38 String WSDL_URI = "http://100702b5.nat123.cc:38965/Service/ServiceHello?wsdl"; 39 40 Cursor cursor; 41 publishProgress(arg0); 42 /* 备份通讯录 */ 43 cursor = db.query("Contact", null, null, null, null, null, null); 44 if (cursor.moveToFirst()) { 45 do { 46 String name = cursor.getString(cursor.getColumnIndex("name")); 47 String telephone = cursor.getString(cursor 48 .getColumnIndex("telephone")); 49 String contact = "{\"name\":" + "\"" + name + "\"" + "," 50 + "\"telephone\":" + "\"" + telephone + "\"}"; 51 Log.d("debug", contact); 52 SoapObject request = new SoapObject(namespace, methodName); 53 request.addProperty("arg0", "contact"); 54 request.addProperty("arg1", contact); 55 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 56 SoapSerializationEnvelope.VER11); 57 envelope.setOutputSoapObject(request); 58 envelope.bodyOut = request; 59 envelope.dotNet = false; 60 HttpTransportSE ht = new HttpTransportSE(WSDL_URI); 61 62 try { 63 Log.d("debug", "调用webservice中........."); 64 ht.call(null, envelope); 65 // 获取返回的数据 66 SoapObject object = (SoapObject) envelope.bodyIn; 67 // 获取返回的结果 68 String result = object.getProperty(0).toString(); 69 Log.d("debug", result); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 flag = false; 73 } 74 } while (cursor.moveToNext()); 75 } 76 cursor.close();197 198 @Override 199 protected void onPostExecute(Boolean result) { 200 progressDialog.dismiss(); 201 if(flag){ 202 Toast.makeText(MyApplication.getContext(), "备份成功", Toast.LENGTH_SHORT).show(); 203 }else{ 204 Toast.makeText(MyApplication.getContext(), "备份失败", Toast.LENGTH_SHORT).show(); 205 } 206 } 207 208 }
不过还是没搞明白为什么webservice局域网无法访问....
Android局域网访问webservice以及其中的一些问题
原文:http://www.cnblogs.com/f91og/p/6396039.html