将我的Sqlite数据上传到SqlServer JSON

时间:2018-02-06 06:11:04

标签: java android json sqlite android-asynctask

使用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;
}

但是我得到错误在json数组[0]为空这里是错误

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)

有人可以告诉我哪里做错了。 ?

3 个答案:

答案 0 :(得分:1)

我manege使用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

答案 1 :(得分:0)

您无法使用new JSONArray(userList)将任意POJO对象转换为JSON。在引擎盖下,它使用JSONObject#wrap()来尝试将Collection条目转换为JSONObject,但对于POJO,它将只返回空值,使JSONArray处于不一致状态。

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

答案 2 :(得分:-1)

您是否添加了网络权限? <uses-permission android:name="android.permission.INTERNET" />