我拼命地試圖解決與我的Android應用程序的問題。我將一個列表提交給我的服務器,並將枚舉設置爲'標記'。 PHP頁面應該查看這個標籤,然後繼續執行相關的函數並作爲json數組或對象返回。這適用於應用程序的一個版本,但克隆版本無法獲取數據。 PHP只是直接跳過標記檢查isset標記並且標記不是空的條件,所以它必須完全看到一個空的POST或我提交的對象不符合我不知道的某些要求。
我查看了這麼多的帖子,搜索和搜索,但還沒有找到解決方案。它爲什麼會對一個版本的應用程序有效,但對於升級版本不起作用,它沒有對用於發送數據的方法進行任何更改?
所以這就是我正在處理的。首先,一個的AsyncTask需要的對象,並傳遞到處理一類的通信:
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);
}
}
}
使用CloudConnect類來獲得單個JSON對象或對象的數組:
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;
}
而PHP代碼檢查標籤:
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);
}
我聽不懂你的問題是什麼。你可以再詳細一點嗎? –
當然。對不起,如果不明確。我的應用程序從服務器發送一個JSON對象的HttpPost請求。 (應用程序 - > PHP - > SQL - > PHP - > JSON編碼 - >應用程序)我可以通過使用登錄Eclipse來顯示對象值使用EntityUtils.toString(post.getEntity() )。但是,服務器的響應始終是發送標記爲空的錯誤。 PHP有一個檢查來驗證標籤在開始時不爲空或空,因此它可以執行函數。然而,儘管HttpPost的對象顯示爲有效,它總是會看到一個空或空標記。 – fakataha