I am trying to send a JSON post array to a PHP server. The array contains separate objects/tickets that PHP will decode. Android loads the data into the JSON object from its local database just fine. I can log the data it's pulling and see it before it's sent to the PHP server. On the server side the data doesn't make it. It's sending an empty array.
My code:
public void sendQueuedTickets() {
// Check queue for tickets.
if (dbc.checkQueTickets() != null) {
String[] userInfo;
Cursor cursor = dbc.checkQueTickets();
int columns = cursor.getColumnCount();
int rows = cursor.getCount();
final ArrayList<Integer> queueIDs = new ArrayList<>();
JSONArray arr = new JSONArray();
cursor.moveToFirst();
// Get user info.
userInfo = dbc.checkCredentials();
// Add user info and request type to params so server knows what we're doing.
JSONObject jobj = new JSONObject();
try {
jobj.put("username", userInfo[0]);
jobj.put("serverUserID", userInfo[2]);
jobj.put("request", "submitTicket");
} catch (JSONException e) { e.printStackTrace(); }
// Add SQL tickets to params to be sent.
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
try {
jobj.put(cursor.getColumnName(c), cursor.getString(c));
} catch (JSONException e) { e.printStackTrace(); }
}
arr.put(jobj);
queueIDs.add(cursor.getInt(0)); // id column value.
cursor.moveToNext();
}
// Send ticket(s) to remote server.
// Set timeout parameters.
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
// Continue with http request.
HttpClient client = new DefaultHttpClient(httpParameters);
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://" + userInfo[3] + "." + dbc.REMOTE_SERVER_URL);
try {
StringEntity se = new StringEntity(arr.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(httpPost, httpContext);
String resFromServer = org.apache.http.util.EntityUtils.toString(response.getEntity());
System.out.println(resFromServer);
// Delete queued ticket(s) if sent transaction successful.
if (resFromServer.contains("successfulSubmit")) {
for (int r = 0; r < rows; r++) {
dbc.deleteQueTicket(queueIDs.get(r));
}
}
} catch (Exception e) { e.printStackTrace(); }
}
}