将我的 Sqlite 数据上传到 SqlServer JSON

Posted

技术标签:

【中文标题】将我的 Sqlite 数据上传到 SqlServer JSON【英文标题】:Upload my Sqlite Data to SqlServer JSON 【发布时间】:2018-07-16 03:11:41 【问题描述】:

使用 HttpURLConnection 发出 HTTP 请求的代码。

 try 
        ds = new DataSource(cont);
        final URL url = new URL("http://sms.abc.com:1980/MOB/SendDoc.aspx");
        con = (HttpURLConnection) url.openConnection();

        con.setDoInput(true);
        con.setDoInput(true);
        con.setChunkedStreamingMode(0);
        con.addRequestProperty("Content-Type","application/json; charset=utf-8");
        con.setRequestMethod("POST");

        try
            ds.open(); //Open Data connection

        catch(Exception ex)
            Log.v("jarvis", ex.getMessage());
        finally 
            ds.open();
        
        List<EmpAttandance> userList = new LinkedList<>();

        userList = ds.sendSMS();

        EmpAttandance emp = new EmpAttandance();
        JSONArray JA = new JSONArray(userList);
        for (int i=0;i <JA.length();i++)
            JSONObject JO = (JSONObject) JA.get(i);
            emp = userList.get(i);
            JO.put("DocID1",emp.getEMP_ID());//DocID1variable that are present in web Asp
            JO.put("EMPID1",emp.getDOC_ID());
            JO.put("DATE1",emp.getDATE());
            JO.put("PHONENUM1",emp.getPHONE_NUM());
            JO.put("LON1",emp.getLOC_LON());
            JO.put("LAT1",emp.getLOC_LAT());
            JO.put("SA1",emp.getSA());
            JO.put("COID1",emp.getCO_ID());


            OutputStream out = new BufferedOutputStream(con.getOutputStream());
            out.write(JO.toString().getBytes());
            out.flush();
            out.close();
        
        ds.close();


        int result =con.getResponseCode();
        if (result==200)
            InputStream in = new BufferedInputStream(con.getInputStream());
            reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            String line= null;

            while ((line = reader.readLine()) != null)
                Status=line;
            
        

       catch (Exception e) 
            e.printStackTrace();
        

SQLITE 数据查询:

  public List<EmpAttandance> sendSMS() 
    List<EmpAttandance> personLinkedList = new LinkedList<>();

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String query = "SELECT * FROM " + DatabaseHelper.TBL_DOCATTANDANCE;
    Cursor cursor = db.rawQuery(query,null);
    EmpAttandance emp;
    if (cursor.moveToFirst()) 
        do 
            emp = new EmpAttandance();
            emp.setDOC_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DI_USER_ID)));
            emp.setPHONE_NUM(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_PHONE_NUM)));
            emp.setCO_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_CO_ID)));
            emp.setSA(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_SA)));
            emp.setDATE(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DATE)));
            emp.setEMP_ID(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_EMP_ID)));
            emp.setLOC_LAT(cursor.getDouble(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LAT)));
            emp.setLOC_LON(cursor.getDouble(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LON)));
            personLinkedList.add(emp);
         while (cursor.moveToNext());
    
    return personLinkedList;

但我得到错误 On json Array[0] is null 这是错误

V/jarvis: Connection Checkcom.android.okhttp.internal.huc.HttpURLConnectionImpl:http://sms.iblhc.com:1980/MOB/SendDoc.aspx
V/jarvis: POSTcom.android.okhttp.internal.huc.HttpURLConnectionImpl:http://sms.iblhc.com:1980/MOB/SendDoc.aspx
V/jarvis: list[]
V/jarvis: userList Data array[com.example.zed.androidapplication.EmpAttandance@bcaffce, com.example.zed.androidapplication.EmpAttandance@6b3deef, com.example.zed.androidapplication.EmpAttandance@45cedfc, com.example.zed.androidapplication.EmpAttandance@8fc4c85, com.example.zed.androidapplication.EmpAttandance@69b4ada, com.example.zed.androidapplication.EmpAttandance@f897b0b, com.example.zed.androidapplication.EmpAttandance@688ede8]
W/System.err: org.json.JSONException: Value at 0 is null.
W/System.err:     at org.json.JSONArray.get(JSONArray.java:289)
W/System.err:     at com.example.zed.androidapplication.SendDocAttd.doInBackground(SendDocAttd.java:67)
W/System.err:     at com.example.zed.androidapplication.SendDocAttd.doInBackground(SendDocAttd.java:22)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:305)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:761)

谁能告诉我哪里做错了。 ?

【问题讨论】:

【参考方案1】:

我可以使用 VolLey 将我的 Sqlite 数据上传到远程服务器。它的工作非常好。

    //region Sending
    try
        ds = new DataSource(cont);
        ds.open();
        final Cursor cursor = ds.send();
        if(cursor.moveToFirst())
        do 

            //region Sending Variables
            final String CELL_NO = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_PHONE_NUM));
            final String M_DATE = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DATE));
            final String M_TIME =cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_TIME));
            final String EMP_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_EMP_ID));
            final String UserID = String.valueOf(LoginActivity.ID) ;
            final String DOC_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_DOC_ID));
            final String SA= cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_SA));
            final String NOTE = " - ";
            final String CO_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_CO_ID));
            final String LAT =cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LAT));
            final String LNG=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DA_LOC_LON));
            //endregion

            //region Sending Data
            Log.v("jarvis","Line = 1");
            StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                    new Response.Listener<String>()
                        @Override
                        public void onResponse(String response) 
                            ds.updateStatus(DOC_ID,M_DATE,M_TIME,EMP_ID); //true
                            Toast.makeText(cont,"Uploaded And Saved.." ,Toast.LENGTH_SHORT).show();
                            Log.v("jarvis","Line = 2");
                        
                    
                    , new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 
                    Log.v("jarvis","Line = 3");
                    ds.updateFalse(DOC_ID,M_DATE,M_TIME,EMP_ID); //true
                    Toast.makeText(cont,"Error.." ,Toast.LENGTH_SHORT).show();
                    error.printStackTrace();
                
            )

                @Override
                protected Map<String, String> getParams() throws AuthFailureError 
                    Log.v("jarvis","Line = 4");

                    Map<String, String> param = new HashMap<String, String>();
                    param.put("CELL_NO", CELL_NO);
                    param.put("M_DATE", M_DATE);
                    param.put("M_TIME", M_TIME);
                    param.put("EMP_ID", EMP_ID);
                    param.put("DOC_ID", DOC_ID);
                    param.put("SA", SA);
                    param.put("NOTE", NOTE);
                    param.put("CO_ID", CO_ID );
                    param.put("LAT", LAT);
                    param.put("LNG",LNG);
                    param.put("USER_ID",UserID);
                    return param;
                
            ;
            Log.v("jarvis","Line = 5");
            MySingleton.getmInstance(cont).addToRequestQue(stringRequest);
            //endregion
            Log.v("jarvis","Line = 6");

        while (cursor.moveToNext());
    catch(Exception e)
    //endregion

【讨论】:

【参考方案2】:

您不能使用new JSONArray(userList) 将任意 POJO 对象转换为 JSON。在后台,它使用JSONObject#wrap() 尝试将Collection 条目转换为JSONObjects,但对于POJO,它只会返回空值,使您的JSONArray 处于不一致状态。

如果您想继续使用 org.json 库,您需要自己编写 JSON 序列化代码。

【讨论】:

先生..您能指导我如何为受尊敬的代码编写Json序列化吗?【参考方案3】:

您是否添加了网络权限? &lt;uses-permission android:name="android.permission.INTERNET" /&gt;

【讨论】:

以上是关于将我的 Sqlite 数据上传到 SqlServer JSON的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 XCode 菜单而不是以编程方式将我的 sqlite 文件从文档目录复制到应用程序包

将我的应用程序的 sqlite db 替换为 android 30+ 中的备份数据库

Sql Server数据库数据导入到SQLite数据库中

如果我不能将我的 SQLite 数据库放在我的资源中,我可以将它放在我的 QT 应用程序中的啥位置?

如何将我的 Django SQLite 数据库内容放入 Heroku PostGreSQL 数据库?

Alamofire v4,Swift v3 将 Sqlite 文件上传到域