如何在Parse的android sdk上从saveAllInBackground获取objectId?
我需要在Parse.com中批量保存一些对象,它工作得很好,但我无法使用传统方法获取对象ID。我正在使用savellinbackroung,传递ParseObjects的ArrayList。数据已保存,回调成功,但ArrayList中的my ParseObjects不会获得任何额外数据,包括objectId如何在Parse的android sdk上从saveAllInBackground获取objectId?,android,parse-platform,objectid,Android,Parse Platform,Objectid,我需要在Parse.com中批量保存一些对象,它工作得很好,但我无法使用传统方法获取对象ID。我正在使用savellinbackroung,传递ParseObjects的ArrayList。数据已保存,回调成功,但ArrayList中的my ParseObjects不会获得任何额外数据,包括objectId objectsToUpload = new ArrayList<ParseObject>(); c
objectsToUpload = new ArrayList<ParseObject>();
cursor.moveToFirst();
int titleIndex = cursor.getColumnIndex(Items.TITULO);
int subtitleIndex = cursor.getColumnIndex(Items.SUBTITULO);
int syncedIndex = cursor.getColumnIndex(Items.SYNCED);
int dateIndex = cursor.getColumnIndex(Items.DATE);
int entryIdIndex = cursor.getColumnIndex(Items.ENTRY_ID);
int idIndex = cursor.getColumnIndex(Items._ID);
while (!cursor.isAfterLast()) {
String title = cursor.getString(titleIndex);
String subtitle = cursor.getString(subtitleIndex);
String date = cursor.getString(dateIndex);
int syncedInt = cursor.getInt(syncedIndex);
ParseObject object = new ParseObject(Constants.PARSE_OBJ_NAME);
object.put(Items._ID, idIndex);
object.put(Items.TITULO, title);
object.put(Items.SUBTITULO, subtitle);
object.put(Items.SYNCED, true);
object.put(Items.DATE, date);
objectsToUpload.add(object);
cursor.moveToNext();
}
ParseObject.saveAllInBackground(objectsToUpload, new SaveCallback() {
@Override
public void done(ParseException e) {
if ( e == null )
{
for ( int i =0; i < objectsToUpload.size(); i++ )
{
ParseObject obectUploaded = objectsToUpload.get( i );
String objId = objectsToUpload.get( i ).getObjectId().toString();
}
}
}
});
objectsToUpload=newarraylist();
cursor.moveToFirst();
int titleIndex=cursor.getColumnIndex(Items.TITULO);
int subtituteindex=cursor.getColumnIndex(Items.SUBTITULO);
int syncedIndex=cursor.getColumnIndex(Items.SYNCED);
int dateIndex=cursor.getColumnIndex(Items.DATE);
int entryIdIndex=cursor.getColumnIndex(Items.ENTRY\u ID);
int-idIndex=cursor.getColumnIndex(Items.\u-ID);
而(!cursor.isAfterLast()){
字符串标题=cursor.getString(titleIndex);
String subtitle=cursor.getString(subtitleIndex);
String date=cursor.getString(dateIndex);
int syncedInt=cursor.getInt(syncedIndex);
ParseObject对象=新的ParseObject(Constants.PARSE_OBJ_NAME);
对象。放置(项。\u ID,idIndex);
object.put(Items.TITULO,title);
object.put(Items.SUBTITULO,subtitle);
object.put(Items.synched,true);
object.put(Items.DATE,DATE);
objectsToUpload.add(对象);
cursor.moveToNext();
}
saveAllInBackground(objectsToUpload,newsaveCallback()){
@凌驾
公共作废完成(Parsee异常){
如果(e==null)
{
对于(int i=0;i
字符串objId返回null。在调试过程中,objectsToUpload中的ParseObject除了我之前输入的信息之外没有任何内容 我在Android的Parse SDK中找不到合适的解决方案,尽管我通过使用凌空库的RESTApi解决了这个问题。 它工作正常,我可以恢复保存对象的ID 我推荐使用凌空截击,因为这是一种非常简单的发送和接收JSONObject的方法,所有的后台工作都为您完成。掌握窍门可能有点棘手,但你可以研究一下 我的解决方案的一小部分
final Cursor cursor = fetchUnSynced();
if (cursor != null) {
cursor.moveToFirst();
int titleIndex = cursor.getColumnIndex(SyncAdapterContract.Items.TITULO);
int idIndex = cursor.getColumnIndex(SyncAdapterContract.Items._ID);
JSONObject dataToUpload = new JSONObject(); // The complete data to upload
JSONArray requestsArray = new JSONArray(); // Objects list
//Strings defined at parse.com
final String url = "https://api.parse.com/1/batch";
final String REQUESTS = "requests";
final String METHOD = "method";
final String PATH = "path";
final String BODY = "body";
final String pathUrl = "/1/classes/VolleyTest"; //Change to suit you needs
final String req_tag = "parse_batch";
//Iterate
while (!cursor.isAfterLast()) {
String title = cursor.getString(titleIndex);
JSONObject requestObj = new JSONObject(); //Request for each object
JSONObject bodyObjs = new JSONObject(); //Data of object
try {
//Body
bodyObjs.put(SyncAdapterContract.Items.TITULO, title);
//whatever else you have to upload at each object
//Request
requestObj.put(METHOD, "POST");
requestObj.put(PATH, pathUrl);
requestObj.put(BODY, bodyObjs);
requestsArray.put( requestObj );
} catch (JSONException e) {
e.printStackTrace();
}
cursor.moveToNext();
}
try {
dataToUpload.put(REQUESTS, requestsArray);
} catch (JSONException e) {
e.printStackTrace();
}
JsonArrayRequest jsonArrayReq = new JsonArrayRequest(
Request.Method.POST, url, dataToUpload,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Response with the ids and status of each object
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Error
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put( "X-Parse-REST-API-Key", Constants.PARSE_API_KEY );
headers.put( "X-Parse-Application-Id", Constants.PARSE_APP_ID );
return headers;
}
};
VolleySingleton.getInstance(MyApp.getsInstance()).addToRequestQueue(jsonArrayReq, req_tag );
final Cursor=fetchUnSynced();
如果(光标!=null){
cursor.moveToFirst();
int titleIndex=cursor.getColumnIndex(SyncAdapterContract.Items.TITULO);
int-idIndex=cursor.getColumnIndex(SyncAdapterContract.Items.\u-ID);
JSONObject dataToUpload=new JSONObject();//要上载的完整数据
JSONArray requestsArray=新的JSONArray();//对象列表
//在parse.com上定义的字符串
最终字符串url=”https://api.parse.com/1/batch";
final String REQUESTS=“REQUESTS”;
final String METHOD=“METHOD”;
最终字符串路径=“路径”;
最终字符串BODY=“BODY”;
最后一个字符串pathUrl=“/1/classes/VolleyTest”//根据需要进行更改
最终字符串req_tag=“parse_batch”;
//迭代
而(!cursor.isAfterLast()){
字符串标题=cursor.getString(titleIndex);
JSONObject requestObj=新建JSONObject();//每个对象的请求
JSONObject bodyObjs=新建JSONObject();//对象的数据
试一试{
//身体
bodyObjs.put(SyncAdapterContract.Items.TITULO,标题);
//在每个对象上必须上载的其他内容
//请求
请求对象put(方法,“POST”);
requestObj.put(路径,路径URL);
requestObj.put(BODY,bodyObjs);
requestsArray.put(requestObj);
}捕获(JSONException e){
e、 printStackTrace();
}
cursor.moveToNext();
}
试一试{
dataToUpload.put(请求,requestsArray);
}捕获(JSONException e){
e、 printStackTrace();
}
JsonArrayRequest jsonArrayReq=新JsonArrayRequest(
Request.Method.POST、url、dataToUpload、,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//使用每个对象的ID和状态进行响应
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//错误
}
}
) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“X-Parse-REST-API-Key”,Constants.Parse_-API_-Key);
headers.put(“X-Parse-Application-Id”,Constants.Parse\u APP\u Id);
返回标题;
}
};
截击