I need send arrayList of objects from sqlite for server. I've tried many ways, not works. If anyone can help me I will be very grateful.
This is my method findAllTanques in my DAO class
public List<Tanque> buscarTodosTanques() {
List<Tanque> tanques = new ArrayList<Tanque>();
Cursor cursor = database.query("Tanque", new String[]{"_id", "data", "comprimento", "largura", "profundidade", "tipo_tanque", "fase_cultivo"}, null, null, null, null, "_id");
if (cursor.getCount() >= 0) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Tanque tanque = Tanque.getInstance();
tanque.setId(cursor.getLong(0));
tanque.setData(cursor.getString(1));
tanque.setComprimento(cursor.getFloat(2));
tanque.setLargura(cursor.getFloat(3));
tanque.setProfundidade(cursor.getFloat(4));
tanque.setTipo_tanque(cursor.getInt(5));
tanque.setFase_cultivo(cursor.getString(6));
tanques.add(tanque);
cursor.moveToNext();
}
}
cursor.close();
return tanques;
}
This is my CustomArrayRequest class
public class NewCustomJsonArrayRequest extends Request<JSONArray> {
private Listener<JSONArray> response;
private Map<String, String> params;
private Token token;
public NewCustomJsonArrayRequest(int method, String url, Map<String, String> params, Listener<JSONArray> response, ErrorListener listener) {
super(method, url, listener);
this.params = params;
this.response = response;
this.token = Token.getInstance();
// TODO Auto-generated constructor stub
}
public NewCustomJsonArrayRequest(String url, Map<String, String> params, Listener<JSONArray> response, ErrorListener listener) {
super(Method.GET, url, listener);
this.params = params;
this.response = response;
// TODO Auto-generated constructor stub
}
public Map<String, String> getParams() throws AuthFailureError{
return params;
}
public Map<String, String> getHeaders() throws AuthFailureError{
HashMap<String, String> header = new HashMap<String, String>();
header.put("Authorization", "Bearer " + token.getAccessToken());
return(header);
}
public Priority getPriority(){
return(Priority.NORMAL);
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Check if it is JSONObject or JSONArray
Object json = new JSONTokener(jsonString).nextValue();
JSONArray jsonArray = new JSONArray();
if (json instanceof JSONObject) {
jsonArray.put(json);
} else if (json instanceof JSONArray) {
jsonArray = (JSONArray) json;
}
return Response.success(jsonArray, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(JSONArray response) {
this.response.onResponse(response);
}
}
This is my method sendArrayTanques in my fragment class
public void enviarTanqueArray() {
String url = "http://myUrl/";
tanqueDAO.buscarTodosTanques();
params = new HashMap<String, String>();
params.put("largura", Float.toString(tanque.getLargura()));
params.put("tipo_tanque", Integer.toString(tanque.getTipo_tanque()));
params.put("comprimento", Float.toString(tanque.getComprimento()));
params.put("profundidade", Float.toString(tanque.getProfundidade()));
NewCustomJsonArrayRequest jsonArrayRequest = new NewCustomJsonArrayRequest(Request.Method.POST, url, params,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.i("Script", "SUCCESS: " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
jsonArrayRequest.setTag("tag");
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
}
Aucun commentaire:
Enregistrer un commentaire