2016-10-03 40 views
0

我想發送整個(50K行)MySQL數據庫服務器數據的GPS座標及其信息到Android手機使用json(13.8 MB)第一次運行我的應用程序,我有這麼多的重大問題!從php mysql服務器加載大型JSON數據到java安卓客戶端

我曾經從PHP yii2控制器發送JSON數據的代碼是:

public function actionGetGPS(){ 
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 
     Yii::$app->response->data["success"] = true; 
     Yii::$app->response->data["gps"] = \app\models\gps::findAll(); 
} 

,並在Android部分的AsyncTask得到JSON文件,我用此代碼:

public static class getGpsDataForFirstTimeRun extends AsyncTask<String, String, JSONObject> { 
    @Override 
    protected void onPreExecute() {} 

    @Override 
    protected JSONObject doInBackground(String... args) { 
     try { 
      HashMap<String, String> params = new HashMap<>(); 
      // App.URL_GET_GPS = "http://localhost/myproject/web/gps/get-gps" 
      JSONObject jsonObject = JSONParser.MakeHttpRequest(App.URL_GET_Gps, "POST", params); 
      if (jsonObject != null) { 
       try { 
        if (jsonObject.getBoolean(App.TAG_SUCCESS)) { 
         App.Log("Saving Json Data"); 

         JSONArray jsonArray = jsonObject.getJSONArray("Gps"); 

         for (int i = 0; i < (jsonArray.length()); i++) { 
          JSONObject jObject = (JSONObject)jsonArray.get(i); 

          // App.db = new DBHelper(); 
          App.db.addGps(jObject.getInt("id"), jObject.getInt("city_id"), jObject.getDouble("latitude"), jObject.getDouble("longitude")); 
          App.Log("Inserted: "+jObject.toString()); 
         } 

         App.Log("Json Data Saved"); 
        } 
       } 
       catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(JSONObject jsonObject) {} 
} 

並在dbhelper我有這個功能,用於將GPS:

public void addGps(int Gps_id, int city, double latitude, double longitude) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(GPS_COLUMN_ID, Gps_id); 
    values.put(GPS_COLUMN_CITY_ID, city); 
    values.put(GPS_COLUMN_LATITUDE, latitude); 
    values.put(GPS_COLUMN_LONGITUDE, longitude); 
    db.insert(GPS_TABLE_NAME, null, values); 
    //db.close(); // Closing database connection // Updated Question 
    App.Log("New Gps inserted into SQLite: " + Gps_id); 
} 

問題1:PHP服務器存儲器:的134217728個字節允許存儲器大小耗盡

解決方案1:我使用此行代碼使它無限:

ini_set('memory_limit', '-1'); 

問題2:不過,我發現這是不切實際的,如果你有+1000用戶,服務器可能會嗆..

解決方案2:我發現的唯一方法是每24小時取一次數據,並將其放入服務器上的json文件中,如果它在數據庫中發生更改,我將運行查詢以更新它..等等因此,用戶下載應用程序時,將獲得最新的數據,老用戶可以從數據庫中下載新的數據,因爲它是小的變化..

我的問題:這個方法是好的?或者有更好的?

問題3:Android的部分:(50K數據沒有被插入到SQLite的,我得到這個錯誤:

10-03 17:47:39.381 702-702/com.myproject.gps E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.myproject.gps, PID: 702 
    java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed. 
     at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:1027) 
     at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:742) 
     at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:397) 
     at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:905) 
     at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834) 
     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62) 
     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:147) 
     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:136) 
     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197) 
     at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237) 
     at com.myproject.gps.Tools.Instance.DBHelper.getGps(DBHelper.java:337) 
     at com.myproject.gps.Dashboard.GPSMapFragment.onCameraIdle(GPSMapFragment.java:119) 
     at com.google.android.gms.maps.GoogleMap$21.onCameraIdle(Unknown Source) 
     at com.google.android.gms.maps.internal.zzf$zza.onTransact(Unknown Source) 
     at android.os.Binder.transact(Binder.java:380) 
     at uv.a(:com.google.android.gms.DynamiteModulesB:79) 
     at maps.ad.j.b(Unknown Source) 
     at maps.ad.j.a(Unknown Source) 
     at maps.ad.j$2.run(Unknown Source) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:145) 
     at android.app.ActivityThread.main(ActivityThread.java:5951) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

解決方案3:我試圖把JSON文件:好像我下載了它,在資產的文件夾,並試圖從那裏插入JSON數據:它的工作,但

問題4:花了像15分鐘,插入50000個JSON數據到sqlite的..

依然沒有找到一個解決方案這!!任何建議或意見將不勝感激..

+1

爲什麼每次插入後關閉sqlite數據庫?這就像是開車去商店購買雜貨,買一罐牛奶,開車回家,把它放在冰箱裏,然後開車回商店,買一塊麪包,開車回家等等......大大無效。 –

+0

哇,我沒有想到這個..讓我試試:) –

+1

1.使用一些json streaming API.2。使用事務3.做更好的同步 - FX把最後修改時間戳到行,並下載只有行時間戳大於請求給出的行... – Selvin

回答

2

是否可以讓您的應用程序逐個下載GPS數據?對於第一次安裝,可能只有10000行座標距離用戶最近。然後您可以按計劃在後臺運行剩餘數據更新。

+0

我已經這樣做了,這只是一個城市..如果我想下載整個國家,它將是400,000 :( –

相關問題