Volley从游标循环添加字符串请求

时间:2018-10-04 22:53:52

标签: android android-volley android-cursor

我正在尝试通过查询游标循环将Contacts数据备份到MySQL,该游标循环将值传递给凌空单例。我的代码:

//global data variables
Cursor phones =null;

private String device_id;
private String name;
private String phone_number;
private String last_contacted;
private String times_contacted;
private String last_updated;
...
//contacts query code
phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,"\"contact_last_updated_timestamp\">?",wrapStrToArray("1538523345522"), ContactsContract.Contacts.DISPLAY_NAME);

    while (phones.moveToNext())
    {
        name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        phone_number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        last_contacted = formatTimestamp(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED)));
        times_contacted = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED));
        last_updated = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP));//use to synchronize without formatting

        saveContactToServer();

    }
    phones.close();// close cursor
...
//string request, add to queue code
    private void saveContactToServer() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVE_CONTACT,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject obj = new JSONObject(response);
                        if (!obj.getBoolean("error")) {
                            //if there is a success
                            //storing the name to sqlite with status synced
                            saveContactToLocalStorage(device_id, name, phone_number, last_contacted, times_contacted, last_updated, CONTACT_SYNCED_WITH_SERVER);
                        } else {
                            //if there is some error
                            //saving the name to sqlite with status unsynced
                            saveContactToLocalStorage(device_id, name, phone_number, last_contacted, times_contacted, last_updated, CONTACT_NOT_SYNCED_WITH_SERVER);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    //on error storing the name to sqlite with status unsynced
                    saveContactToLocalStorage(device_id, name, phone_number, last_contacted, times_contacted, last_updated, CONTACT_NOT_SYNCED_WITH_SERVER);
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("device_id", device_id);
            params.put("name", name);
            params.put("phone_number", phone_number);
            params.put("last_contacted", last_contacted);
            params.put("times_contacted", times_contacted);
            params.put("last_updated", last_updated);
            return params;
        }
    };

    VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

如果我通过带有单个记录的游标,则一切按预期进行。如果我从带有4条记录的游标中传递数据,则会在数据库中获得4次插入,但所有插入都来自最后一个游标记录中的数据。我也尝试按照https://developer.android.com/training/volley/requestqueue中所述创建RequestQueue,结果相同,因此请不要转发我到那里。感谢您的任何建议。

0 个答案:

没有答案