使用Web服务来填充本地数据库

时间:2011-09-29 07:56:56

标签: android web-services

您好我正在尝试从.NET Web服务填充我的本地数据库应用程序,但不能正常运行...需要帮助

public class MitsAndroidApplication extends Application {
        private static String SOAP_ACTION = "http://tempuri.org/";
    private static final String NAMESPACE = "http://tempuri.org/";    
    private static final String URL = "http://...../anka/CL3M.asmx";
    public String WSReturnVal ="";

@Override
    public void onCreate() {
        super.onCreate();

        DbHelper dbh = new DbHelper(this);
        SQLiteDatabase db = dbh.getWritableDatabase();
        String sqlString = "SELECT " + Doktorlar.KEY_ROWID + " FROM " + Doktorlar.DATABASE_TABLE  ;
        Cursor aCursor = db.rawQuery(sqlString, null);
        aCursor.moveToFirst();
        int doktorNumber = aCursor.getCount();

        String sqlStringIl = "SELECT " + Iller.KEY_ROWID + " FROM " + Iller.DATABASE_TABLE  ;
        Cursor ilCursor = db.rawQuery(sqlString, null);
        ilCursor.moveToFirst();
        int ilNumber = ilCursor.getCount();

        dbh.close();

        if(doktorNumber == 0) {
            loadDoktorData();
        }
        if(ilNumber == 0) {
            loadilData();
        }

    }
private void loadDoktorData() {

        String [] paramvalues = new String[4];
        paramvalues[0] = "GetDoktors"; paramvalues[1] = "45!ghUvb0PsXc";
        paramvalues[2] = "mummesilid"; paramvalues[3] = "81";
        LoadDoktorsWebServiceTask  task = new LoadDoktorsWebServiceTask();
        task.execute(paramvalues); //Doktorlar artık WSReturnVal da

    }

    private void loadilData() {

        String [] paramvalues = new String[4];
        paramvalues[0] = "GetIller"; paramvalues[1] = "45!ghUvb0PsXc";
        LoadIllerWebServiceTask task = new LoadIllerWebServiceTask();
        task.execute(paramvalues); //Iller artık WSReturnVal da

    }


     private class LoadDoktorsWebServiceTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
                String response = "";
                    try {
                        //for (String url : urls) {

                        Log.i("ABC", "do in bg doktor ws");
                        DbHelper dbh = new DbHelper(getApplicationContext());
                        SQLiteDatabase db = dbh.getWritableDatabase();

                        String yontemadi = urls[0].toString();
                        SOAP_ACTION = NAMESPACE + yontemadi;
                        SoapObject request = new SoapObject(NAMESPACE, yontemadi);
                        request.addProperty("pass", "45!ghUvb0PsXc");
                        if(urls.length>2) //demekki baska parametreler var , once parametreadi sonrada value bu array icinde geliyor. s˝ral˝ olarak
                        {
                            for(int i=2; i<=urls.length-2; i=i+2)
                            {                       
                                request.addProperty(urls[i].toString(), urls[i+1].toString());
                            }
                        }

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE httpTransport = new HttpTransportSE(URL);                 
                        httpTransport.call(SOAP_ACTION, envelope);   
                        String result;
                        result = ((SoapPrimitive) envelope.getResponse()).toString();
                        response =  result;
                        WSReturnVal = response;
                        if(WSReturnVal!="")
                        {
                            String ayrac1 = "^", ayrac2="|";        
                            String[] iller = WSReturnVal.split(ayrac1); //birden cok doktor var

                            for(int i=0;i<iller.length;i++)
                            {   Log.i("abc", "do in bg doktor ws before db write");

                                String[] ildegerleri = iller[i].split(ayrac2);
                                //gelen her bir row için            
                                dbh.InsertIl(db, ildegerleri[1], Integer.parseInt(ildegerleri[0]));

                            }
                            //gelen her bir row için 
                            //dbh.InsertIl(db, name, il_id);
                            //çağrılacak
                        dbh.close();
                         }
                        SystemClock.sleep(200);
                    }
                     catch (XmlPullParserException e) {
                         e.printStackTrace();
                     }
                      catch (Exception e) {
                        e.printStackTrace();
                     }

                return response;
            }

            @Override
            protected void onPostExecute(String result) {
                WSReturnVal = "";
                //tv.setText(result);
                Toast.makeText(getApplicationContext(), "Web Service is accessed", Toast.LENGTH_SHORT).show();  
            }
        }

     private class LoadIllerWebServiceTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
                String response = "";
                    try {
                        //for (String url : urls) {
                        Log.i("abc", "do in bg iller ws");


                        String yontemadi = urls[0].toString();
                        SOAP_ACTION = NAMESPACE + yontemadi;
                        SoapObject request = new SoapObject(NAMESPACE, yontemadi);
                        request.addProperty("pass", "45!ghUvb0PsXc");
                        if(urls.length>2) //demekki baska parametreler var , once parametreadi sonrada value bu array icinde geliyor. s˝ral˝ olarak
                        {
                            for(int i=2; i<=urls.length-2; i=i+2)
                            {                       
                                request.addProperty(urls[i].toString(), urls[i+1].toString());
                            }
                        }

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE httpTransport = new HttpTransportSE(URL);                 
                        httpTransport.call(SOAP_ACTION, envelope);   
                        String result;
                        result = ((SoapPrimitive) envelope.getResponse()).toString();
                        response =  result;
                        WSReturnVal = response;
                        if(WSReturnVal!="")
                        {
                            String[] doktorlar;

                            String ayrac1 = "^", ayrac2="|";
                            if(!WSReturnVal.contains(ayrac1)) {
                                doktorlar = new String[1];
                                doktorlar[0] = WSReturnVal;  //tek doktor
                            }
                            else
                                doktorlar = WSReturnVal.split(ayrac1); //birden cok doktor var

                            for(int i=0;i<doktorlar.length;i++)
                            {    
                            DbHelper dbh = new DbHelper(getApplicationContext());
                            SQLiteDatabase db = dbh.getWritableDatabase();
                                String[] doktordegerleri = doktorlar[i].split(ayrac2);
                                //gelen her bir row için            
                                dbh.insertDoktor(db, Integer.parseInt(doktordegerleri[0]), doktordegerleri[1], doktordegerleri[2], doktordegerleri[3], doktordegerleri[4], Integer.parseInt(doktordegerleri[7]), doktordegerleri[5], doktordegerleri[6]);
                            dbh.close();
                            }
                       SystemClock.sleep(200);
                         }

                    }
                     catch (XmlPullParserException e) {
                         e.printStackTrace();
                     }
                      catch (Exception e) {
                        e.printStackTrace();
                     }

                return response;
            }

            @Override
            protected void onPostExecute(String result) {
                WSReturnVal = "";
                //tv.setText(result);
                Toast.makeText(getApplicationContext(), "Web Service is accessed", Toast.LENGTH_SHORT).show();  
            }
        }

}

什么不起作用是没有错误,并且没有结果可以添加到我的本地数据库。 但是在LOgCat上有这个错误

09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):从未在数据库'/data/data/com.tugce.MitsActionBar/databases/MyDB1'上显式调用close() 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.database.sqlite.SQLiteDatabase。(SQLiteDatabase.java:1980) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:977) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at com.tugce.MitsActionBar.MitsAndroidApplication $ LoadDoktorsWebServiceTask.doInBackground(MitsAndroidApplication.java:129) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at com.tugce.MitsActionBar.MitsAndroidApplication $ LoadDoktorsWebServiceTask.doInBackground(MitsAndroidApplication.java:1) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):在android.os.AsyncTask $ 2.call(AsyncTask.java:252) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at java.util.concurrent.FutureTask.run(FutureTask.java:137) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:574) 09-29 11:02:45.218:ERROR / SQLiteDatabase(12529):at java.lang.Thread.run(Thread.java:1020)

1 个答案:

答案 0 :(得分:0)

什么不起作用:WEB服务响应,本地数据库或其他什么? 你能显示你的日志吗? 1)检查Web服务响应(您可以通过浏览器执行:将String打开为.xml)。 2)检查sqlite db(你可以通过模拟器上的eclipse插件来实现)。