2016-10-03 37 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 :( –

相关问题