I'm desperately trying to solve an issue with my android app. I submit a List to my server with an enum set as a 'tag'. The PHP pages should look at this tag and then proceed to perform the associated functions and return as a json array or object. This works fine with one version of the app but a cloned version fails to fetch data. The PHP just jumps straight over tag checking at the isset tag and tag is not empty conditions so it must be flat out seeing an empty POST or the object I submit doesn't meet some requirement I'm unaware of.
I've looked through so many posts and searched and searched but haven't found a solution. Why would it work for one version of the app but not for the upgraded version, that hasn't made any changes to the methods used for sending data??
So here's what I'm dealing with. To begin with, an AsyncTask takes the objects and passes to a class that handles communication:
private class UpdateJobList extends AsyncTask<User, Void, Boolean> {
private List<Message> messages;
public UpdateJobList() {
super();
messages = new ArrayList<Message>();
}
@Override
protected Boolean doInBackground(User... params){
try {
CloudConnect cConn = new CloudConnect(sAddress);
this.messages = cConn.getAll(params[0]);
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (true)
{
handleMessageList(messages);
}
}
}
Using the CloudConnect class to get single Json objects or an array of Objects:
public class CloudConnect {
private String site;
private InputStream is;
private Gson gson;
public CloudConnect(String site) throws MalformedURLException {
this.site = site;
this.gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
is = null;
}
public synchronized Message get(Message m) throws IOException {
Message msg = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(this.site);
post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));
HttpResponse response = client.execute(post);
StatusLine status = response.getStatusLine();
if ( status.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
is = entity.getContent();
try {
Reader read = new InputStreamReader(is);
String str = (String) gson.fromJson(read, Object.class);
JsonParser parser = new JsonParser();
JsonElement jElem = parser.parse(str);
JsonObject jObject = (JsonObject) jElem;
msg = gson.fromJson(jObject, Message.class);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return msg;
}
public synchronized List<Message> getAll(Message m) throws IOException {
List<Message> mList = new ArrayList<Message>();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(this.site);
post.setEntity(new UrlEncodedFormEntity(validateMessage(m)));
HttpResponse response = client.execute(post);
StatusLine status = response.getStatusLine();
if ( status.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
is = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
JsonArray jArray = null;
JsonReader jReader = new JsonReader(reader);
jReader.setLenient(true);
JsonParser parser = new JsonParser();
if (parser.parse(jReader).isJsonArray()){
jArray = parser.parse(jReader).getAsJsonArray();
if ( m instanceof User ){
for (JsonElement je : jArray) {
mList.add(gson.fromJson(je, Job.class));
Log.d("json", je.toString());
}
} else if ( m instanceof Job ) {
for (JsonElement je : jArray) {
mList.add(gson.fromJson(je, Update.class));
Log.d("json", je.toString());
}
}
} else {
JsonElement jElem = parser.parse(jReader);
JsonObject jObject = (JsonObject) jElem;
Error msg = null;
msg = gson.fromJson(jObject, Error.class);
mList.add(msg);
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return mList;
}
And the PHP code that checks for the tag:
if ( isset($_POST['messageType']) && $_POST['messageType'] != "") {
$tag = $_POST['messageType'];
//
//various functions depending on messageType tag here. Such as getUser($email).
//functions appear to work fine if the PHP doesn't find the initial conditions
//false and skips them all.
} else {
$response["success"] = 0;
$response["error"]["errorMsg"] = "Tags are null";
$response["error"]["messageType"] = $tag;
$response["error"]["varDump"] = var_dump($_POST);
echo json_encode($response);
}