使用POST,OKHttp将数据从SQLite发送到appServer

问题描述 投票:1回答:2

我有一个存储通话记录的SQLite Db。每个条目都有一个数字,日期和持续时间。我已经看到有很多方法可以将数据发送到应用服务器。作为JSON字符串并从模型类对象的ArrayList逐个发送。这是解决这个问题的正确方法吗?如何从这些数据创建JSON,我已经完成了将这些数据传递给对象的ArrayList所做的事情。由于每个条目都有很多数据,我对如何做到这一点很困惑。

    public ArrayList<PhNumber> getCallLogs() {

    ArrayList<PhNumber> callLogList;
    callLogList = new ArrayList<PhNumber>();

    String selectQuery = "SELECT  * FROM callInfo where syncStatus = '" + "no" + "'";

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

    if (cursor.moveToFirst()) {
        do {
            callLogList.add(new PhNumber(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4)));

            Log.e("DbHelper:getCallLogs", callLogList.toString());
        } while (cursor.moveToNext());
    }
    return callLogList;
}
android sqlite post okhttp okhttp3
2个回答
0
投票

这是解决这个问题的正确方法吗?

没有任何解决方案。这取决于您的用例场景。

为了制作JSON,您可以在ArrayList<PhNumber> callLogList中获取数据后执行此操作

JSONArray jsonArray = new JSONArray();

  for (int i = 0; i < callLogList.length(); i++) {
 JSONObject jsonobject= new JSONObject();
 jsonobject.put("data1",callLogList.get(i).get); //I dont know the field name of your PhNumber so fill accordingly
 jsonobject.put("data2",callLogList.get(i).get);

 jsonArray.put(jsonobject);
}

0
投票

我建议你使用Retrofit,这是一个非常容易使用的库,节省了大量的时间和代码。使用Retrofit无缝处理序列化和HTTP请求。

please refer to this article for simple understanding of Retrofit

© www.soinside.com 2019 - 2024. All rights reserved.