提交项目
This commit is contained in:
@ -0,0 +1,89 @@
|
||||
package com.gh.common.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.volley.Request.Method;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.constant.Constants;
|
||||
import com.gh.gamecenter.db.DataCollectionDao;
|
||||
import com.gh.gamecenter.db.info.DataCollectionInfo;
|
||||
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
||||
|
||||
public class DataCollectionUtils {
|
||||
|
||||
public static void uploadData(Context context) {
|
||||
final DataCollectionDao dao = new DataCollectionDao(context);
|
||||
List<DataCollectionInfo> list = dao.getAll();
|
||||
if (list.size() >= Constants.DATA_AMOUNT) {
|
||||
String url = "http://114.215.139.210/data/collection/upload";
|
||||
HashMap<String, ArrayList<String>> params = new HashMap<String, ArrayList<String>>();
|
||||
final ArrayList<String> ids = new ArrayList<String>();
|
||||
DataCollectionInfo entity = null;
|
||||
ArrayList<String> data = null;
|
||||
for (int i = 0, size = list.size(); i < size; i++) {
|
||||
entity = list.get(i);
|
||||
ids.add(entity.getId());
|
||||
String type = entity.getType();
|
||||
entity.getData();
|
||||
data = params.get(type);
|
||||
if (data == null) {
|
||||
data = new ArrayList<String>();
|
||||
params.put(type, data);
|
||||
}
|
||||
data.add(entity.getData());
|
||||
}
|
||||
JSONArray body = new JSONArray();
|
||||
JSONObject jsonObject = null;
|
||||
try {
|
||||
for (String key : params.keySet()) {
|
||||
data = params.get(key);
|
||||
jsonObject = new JSONObject();
|
||||
jsonObject.put("type", key);
|
||||
jsonObject.put("data", data);
|
||||
body.put(jsonObject);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
|
||||
Method.POST, url, body.toString(),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
Utils.log(response.toString());
|
||||
try {
|
||||
if ("success".equals(response
|
||||
.getString("status"))) {
|
||||
// 删除数据库数据
|
||||
dao.delete(ids);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Utils.log(error.toString());
|
||||
if (error.networkResponse != null) {
|
||||
Utils.log(new String(error.networkResponse.data));
|
||||
}
|
||||
}
|
||||
});
|
||||
AppController.addToRequestQueue(request, DataCollectionUtils.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user