我正在尝试创建我的第一个Android应用程序,它由jdeveloper 12c开发的两个部分和带有android的前端组成。
我创建了一个soap web服务,以便在android部分使用它,我也创建了一个sqlite数据库,但我现在不知道如何同步数据以便检索它..
这就是我在jdeveloper中创建Web服务的方式
package model.Services;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
import model.Entite;
@Stateless(name = "SessionEJBEntity", mappedName = "Mobile_Survey-Model-SessionEJBEntity")
@WebService(name = "SessionEJBEntityBeanService", portName = "SessionEJBEntityBeanServicePort")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public class SessionEJBEntityBean implements SessionEJBEntity, SessionEJBEntityLocal {
@Resource
SessionContext sessionContext;
@PersistenceContext(unitName = "Model")
private EntityManager em;
public SessionEJBEntityBean() {
}
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@WebMethod (operationName = "querybyrange")
public Object queryByRange(@WebParam(name = "arg0") String jpqlStmt, @WebParam(name = "arg1") int firstResult,
@WebParam(name = "arg2") int maxResults) {
Query query = em.createQuery(jpqlStmt);
if (firstResult > 0) {
query = query.setFirstResult(firstResult);
}
if (maxResults > 0) {
query = query.setMaxResults(maxResults);
}
return query.getResultList();
}
@WebMethod (operationName = "presistentity")
public Entite persistEntite(@WebParam(name = "arg0") Entite entite) {
em.persist(entite);
return entite;
}
@WebMethod (operationName = "mergenetity")
public Entite mergeEntite(@WebParam(name = "arg0") Entite entite) {
return em.merge(entite);
}
@WebMethod
@Oneway
public void removeEntite(@WebParam(name = "arg0") Entite entite) {
entite = em.find(Entite.class, entite.getIdentité());
em.remove(entite);
}
/** <code>select o from Entite o</code> */
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@WebMethod (operationName = "listEntity")
public List<Entite> getEntiteFindAll() {
return em.createNamedQuery("Entite.findAll", Entite.class).getResultList();
}
}
我有这个网址: 本地主机:7101 / Mobile_Survey-模型web应用/ SessionEJBEntityBeanService WSDL
在android中我创建了一个名为database的类package com.example.myfirstapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Database extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String TABLE_ENTITEE= "table_entitée";
private static final String IDENTITÉ ="identité";
private static final String TYPE_ENT="typeentité";
private static final String ID_SECTEUR ="idsecteur";
private static final String LONG_ENT ="longent";
private static final String ATT_ENT ="attent";
private static final String DESC_ENT="descent";
private static final String CREATE_ENTITEE= "CREATE TABLE "
+ TABLE_ENTITEE + " (" + IDENTITÉ
+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TYPE_ENT + " VARCHAR(100),"
+ ID_SECTEUR + " VARCHAR(100)"
+ LONG_ENT + " VARCHAR(100)"
+ ATT_ENT + "VARCHAR(100)"
+ DESC_ENT+ "VARCHAR(300)"
+")";
// Database Name
private static final String DATABASE_NAME = "mobileSurvey.db";
public Database(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ENTITEE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " +TABLE_ENTITEE );
// Create tables again
onCreate(db);
}
}
和一个连接类
package com.example.myfirstapp;
import java.io.InputStream;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import android.util.Log;
public class Connexion {
private InputStream is = null;
private HttpClient httpclient = new DefaultHttpClient();
private HttpResponse response;
private HttpEntity entity;
private BufferedReader reader;
private StringBuilder sb;
private String line;
private String result = "";
public Connexion(String requete,String methode, ArrayList<NameValuePair> parametres){
try{
if (methode.equals("get")){
HttpGet httpget = new HttpGet(requete);
response = httpclient.execute(httpget);
}else{
HttpPost httppost = new HttpPost(requete);
httppost.setEntity(new UrlEncodedFormEntity(parametres));
response = httpclient.execute(httppost);
}
entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
result="[error]";
}catch(OutOfMemoryError E){
Log.i("Erreur",E.toString());
}
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
result="[error]";
}catch(OutOfMemoryError E){
Log.i("Erreur",E.toString());
}
}
public String getResult() {
return result;
}
}
和一个同步数据的类
package com.example.myfirstapp;
import java.util.HashMap;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class SyncBDD {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "mobileSurvey.db";
private static final String TABLE_ENTITEE= "table_entitée";
private SQLiteDatabase bdd;
private Database database;
public SyncBDD(Context context) {
database = new Database(context, NOM_BDD, null, VERSION_BDD);
}
public void open() {
bdd = database.getWritableDatabase();
}
public void close() {
bdd.close();
}
public SQLiteDatabase getBDD() {
return bdd;
}
void insert_entitée(HashMap<String, String>params){
String selectQuery1 = "INSERT INTO " + TABLE_ENTITEE
+ " VALUES (NULL," + "'" + params.get("TYPE_ENT")+
"'" + ", " + "'"
+ params.get("ID_SECTEUR")+ "'" + ")";
Log.e("insert_ingredient --->", selectQuery1);
bdd.execSQL(selectQuery1);
Log.e("gooooooooood --->", "good");
}
public Cursor get_entitée() {
String s = "";
String selectQuery = " select * from " + TABLE_ENTITEE;
Cursor cursor = bdd.rawQuery(selectQuery, null);
return cursor;
}
}
但我阻止了如何通过网络服务传递数据...有人可以帮我吗?