2017-01-13 72 views
0

我正在使用XMLPullParser解析Web服務響應,並且希望將其存儲在本地數據庫中,以便能夠減少網絡流量,因爲API不是最有效的移動。無法保存對數據庫的XML解析響應

我已經創建了數據庫類,不知道如何將響應放入數據庫,然後使用它獲取我的信息我需要填充我的列表視圖或再次點擊服務以獲取更多數據,因爲數據庫不會'沒有。

具有網絡呼叫和ListView

public class MainActivity extends AppCompatActivity { 

    ListView tappedListView, directReportListView; 
    ProgressBar mProgressBar; 
    ArrayList<PeepWithPic> mPeepWithPics = new ArrayList<>(); 
    ListViewAdapter mAdapter; 
    DatabaseHandler databaseHandler; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /* if toolbar is wanted by user uncomment 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar);*/ 

     tappedListView = (ListView) findViewById(R.id.contactListView); 
     directReportListView = (ListView) findViewById(R.id.clickedContactLV); 
     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 
     mProgressBar.setVisibility(View.VISIBLE); 
     mAdapter = new ListViewAdapter(this, mPeepWithPics); 
     tappedListView.setAdapter(mAdapter); 
     directReportListView.setAdapter(mAdapter); 
     getXMLData(); 

     databaseHandler = new DatabaseHandler(mPeepWithPics); // error is here 
    } 

    //Uncomment to add OptionsMenu(three dots on app bar) if needed 
    /*@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    }*/ 

    private static OkHttpClient getUnsafeOkHttpClient() { 
     try { 
      // Create a trust manager that does not validate certificate chains 
      final TrustManager[] trustAllCerts = new TrustManager[]{ 
        new X509TrustManager() { 
         @Override 
         public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { 
         } 

         @Override 
         public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { 
         } 

         @Override 
         public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
          return new java.security.cert.X509Certificate[]{}; 
         } 
        } 
      }; 

      // Install the all-trusting trust manager 
      final SSLContext sslContext = SSLContext.getInstance("SSL"); 
      sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); 
      // Create an ssl socket factory with our all-trusting manager 
      final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); 

      OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
      builder.connectTimeout(5, TimeUnit.MINUTES) 
        .writeTimeout(5, TimeUnit.MINUTES) 
        .readTimeout(5, TimeUnit.MINUTES); 
      builder.sslSocketFactory(sslSocketFactory); 
      builder.hostnameVerifier(new HostnameVerifier() { 
       @Override 
       public boolean verify(String hostname, SSLSession session) { 
        return true; 
       } 
      }); 

      return builder.build(); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public void getXMLData() { 
     OkHttpClient client = getUnsafeOkHttpClient(); 
     Request request = new Request.Builder() 
       .url(getString(R.string.API_FULL_URL)) 
       .build(); 
     client.newCall(request).enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
       e.printStackTrace(); 
      } 

      @Override 
      public void onResponse(Call call, final Response response) throws IOException { 
       final String responseData = response.body().string(); 
       final InputStream stream = new ByteArrayInputStream(responseData.getBytes()); 
       XMLPullParserHandler parserHandler = new XMLPullParserHandler(); 
       final ArrayList<PeepWithPic> peepWithPics = (ArrayList<PeepWithPic>) parserHandler.parse(stream); 
       mPeepWithPics.clear(); 
       mPeepWithPics.addAll(peepWithPics); 


       //tell adapter on the UI thread its data changed 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         mAdapter.notifyDataSetChanged(); 
         tappedListView.setVisibility(View.VISIBLE); 
         directReportListView.setVisibility(View.VISIBLE); 
         mProgressBar.setVisibility(View.GONE); 
        } 
       }); 
      } 
     }); 
    } 

} 

在線路databseHandler =新數據庫處理器(mPeepWithPics)我DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "contactManager"; 
    private static final String TABLE_CONTACTS = "contacts"; 

    //Contacts table columns names 
    private static final String KEY_ID = "Employee_number"; 
    private static final String KEY_FIRST_NAME = "First_name"; 
    private static final String KEY_LAST_NAME = "Last_name"; 
    private static final String KEY_PHONE_NUMBER_MOBILE = "Phone_mobile"; 
    private static final String KEY_PHONE_NUMBER_OFFICE = "Phone_office"; 
    private static final String KEY_PAYROLL_TITLE = "Payroll_title"; 
    private static final String KEY_HAS_DIRECT_REPORTS = "Has_direct_reports"; 
    private static final String KEY_EMAIL = "Email"; 
    private static final String KEY_COST_CENTER = "Cost_center_id"; 


    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" 
       + KEY_ID + " STRING PRIMARY KEY," + KEY_FIRST_NAME + " TEXT," 
       + KEY_LAST_NAME + " TEXT" 
       + KEY_PHONE_NUMBER_MOBILE + " TEXT" 
       + KEY_PHONE_NUMBER_OFFICE + " TEXT" 
       + KEY_PAYROLL_TITLE + " TEXT" 
       + KEY_HAS_DIRECT_REPORTS + " TEXT" 
       + KEY_EMAIL + " TEXT" 
       + KEY_COST_CENTER + " TEXT" + ")"; 
     db.execSQL(CREATE_CONTACTS_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     //drop old table if existence 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); 

     //Create table again 
     onCreate(db); 
    } 

    //Add new contact 
    public void addContact(PeepWithPic peepWithPic) { 
     SQLiteDatabase database = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_FIRST_NAME, peepWithPic.getFirst_name()); 
     values.put(KEY_LAST_NAME, peepWithPic.getLast_name()); 
     values.put(KEY_PHONE_NUMBER_MOBILE, peepWithPic.getPhone_mobile()); 
     values.put(KEY_PHONE_NUMBER_OFFICE, peepWithPic.getPhone_office()); 
     values.put(KEY_HAS_DIRECT_REPORTS, peepWithPic.getHas_direct_reports()); 
     values.put(KEY_EMAIL, peepWithPic.getEmail()); 
     values.put(KEY_COST_CENTER, peepWithPic.getCost_center_id()); 

     //Inserting Row 
     database.insert(TABLE_CONTACTS, null, values); 
     database.close(); 
    } 

    //Get single contact 
    public PeepWithPic getContact(int employeeNumber) { 
     SQLiteDatabase database = this.getReadableDatabase(); 

     Cursor cursor = database.query(TABLE_CONTACTS, new String[] { 
       KEY_ID, KEY_FIRST_NAME, KEY_LAST_NAME, KEY_PHONE_NUMBER_OFFICE, KEY_PHONE_NUMBER_MOBILE, 
       KEY_HAS_DIRECT_REPORTS, KEY_EMAIL, KEY_COST_CENTER}, KEY_ID + "=?", 
       new String[]{ String.valueOf(employeeNumber)}, null, null, null, null); 
     if(cursor != null) 
      cursor.moveToFirst(); 

      PeepWithPic peepWithPic = new PeepWithPic(cursor.getString(0), 
        cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), 
        cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), 
        cursor.getString(9), cursor.getString(10), cursor.getString(11), cursor.getString(12), 
        cursor.getString(12), cursor.getString(14), cursor.getString(15), cursor.getString(16), 
        cursor.getString(17), cursor.getString(18), cursor.getString(19), cursor.getString(20), 
        cursor.getString(21), cursor.getString(22), cursor.getString(23), cursor.getString(24), 
        cursor.getString(24), cursor.getString(25), cursor.getString(26)); 

      return peepWithPic; 
    } 

    //Get All Contacts 
    public List<PeepWithPic> getAllContacts() { 
     List<PeepWithPic> contactList = new ArrayList<>(); 
     //Select all query 
     String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; 

     SQLiteDatabase database = this.getWritableDatabase(); 
     Cursor cursor = database.rawQuery(selectQuery, null); 

     //looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       PeepWithPic peepWithPic = new PeepWithPic(); 
       peepWithPic.setEmployee_number(cursor.getString(7)); 
       peepWithPic.setFirst_name(cursor.getString(9)); 
       peepWithPic.setLast_name(cursor.getString(11)); 
       peepWithPic.setPhone_office(cursor.getString(19)); 
       peepWithPic.setPhone_mobile(cursor.getString(18)); 
       peepWithPic.setHas_direct_reports(cursor.getString(10)); 
       peepWithPic.setEmail(cursor.getString(6)); 
       peepWithPic.setCost_center_id(cursor.getString(4)); 
      } while (cursor.moveToNext()); 
     } 

     //return contat list 
     return contactList; 
    } 

    //Get contact Count 
    public int getContactCount() { 
     String countQuery = "SELECT * FROM " + TABLE_CONTACTS; 
     SQLiteDatabase database = this.getReadableDatabase(); 
     Cursor cursor = database.rawQuery(countQuery, null); 
     cursor.close(); 

     return cursor.getCount(); 
    } 

    //Updating single contact 
    public int updateContact(PeepWithPic peepWithPic){ 
     SQLiteDatabase database = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_FIRST_NAME, peepWithPic.getFirst_name()); 
     values.put(KEY_LAST_NAME, peepWithPic.getLast_name()); 
     values.put(KEY_PHONE_NUMBER_MOBILE, peepWithPic.getPhone_mobile()); 
     values.put(KEY_PHONE_NUMBER_OFFICE, peepWithPic.getPhone_office()); 
     values.put(KEY_HAS_DIRECT_REPORTS, peepWithPic.getHas_direct_reports()); 
     values.put(KEY_EMAIL, peepWithPic.getEmail()); 
     values.put(KEY_COST_CENTER, peepWithPic.getCost_center_id()); 

     return database.update(TABLE_CONTACTS, values, KEY_ID + " = ?", 
       new String[] {String.valueOf(peepWithPic.getEmployee_number())}); 
    } 

    //Delete single contact 
    public void deletedContact(PeepWithPic peepWithPic) { 
     SQLiteDatabase database = this.getWritableDatabase(); 
     database.delete(TABLE_CONTACTS, KEY_ID + " = ?", 
       new String[] {String.valueOf(peepWithPic.getEmployee_number())}); 
     database.close(); 
    } 
} 

MainActivity;我收到一個錯誤,說錯誤:(59,47)錯誤:不兼容的類型:ArrayList不能轉換爲Contex。不知道這甚至意味着什麼。我只是試圖將OkHttp3調用的響應保存到數據庫,以便我可以使用數據庫顯示信息或進行另一次調用,以便在需要時獲取更多數據,因爲此Web服務非常慢。

我將此添加到構造函數中。我知道得到錯誤:(59,72)錯誤:不兼容的類型:ArrayList不能轉換爲PeepWithPic。由於我需要傳遞模型以保存到localDB,但我需要它作爲ArrayList以在ListView適配器中顯示它,所以對於如何傳遞此信息感到困惑。

databaseHandler = new DatabaseHandler(getApplicationContext(), mPeepWithPics); //this is in MainActivity 

private final PeepWithPic peepWithPic; 
public DatabaseHandler(Context context, PeepWithPic peepWithPic) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.peepWithPic = peepWithPic; 
    } 
+0

你的問題有點模糊,或者至少非常寬泛。你堅持什麼部分?你看到錯誤嗎? – nasch

+0

我在底部更新了我的問題。謝謝你指出它不是很清楚。 –

回答

0

I am getting an error that says Error:(59, 47) error: incompatible types: ArrayList cannot be converted to Contex. Not sure what that even means.

您應該考慮做一些基本的Java培訓,使你能夠理解你的IDE是給你的消息。這是告訴你,構造函數期待Context,並且你正試圖通過ArrayList。您需要傳入一個Context,可能是MainActivity實例(this)。

+0

我已將getApplicationContext傳遞給databaseHandler,我可以運行該應用程序。我想要做的是將來自Web服務調用的響應添加到數據庫,但不知道如何做到這一點。我讀過的大多數指南都將一系列信息硬編碼到數據庫中。我有treid databaseHandler.addContact(mPeepWithPics),但我知道這是行不通的,因爲它想要的模型,我想通過它ArrayList –

+0

我不確定你的問題到底在哪裏。您知道如何獲取項目列表到數據庫中?那麼,將服務器響應轉換爲對象列表的問題呢? – nasch

+0

我有一個傳遞peepWithPics到databaseHandler的問題。即使當我在onResponse中解析響應並嘗試將其傳遞給databasehandler.addContact時,我遇到了錯誤,我無法獲取ArrayList並將其轉換爲Model類。我明白爲什麼,但不知道如何將解析的ArrayList onResponse()傳遞給數據庫。 –

0

DatabaseHandler構造函數參數是上下文,但是您注入了ur模型。

改變這一點:

public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

public DatabaseHandler(Context context, Mymodel model) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.model = model; 
    } 
+0

更新了代碼的結尾,我對該錯誤感到困惑。我需要傳遞模型以保存到LocalDB,但需要將其作爲ArrayList填充適配器以供listview使用。 –