0
做大量的研究,我一直沒能在傳遞JSONObject
,然後檢索服務器端找到使用AsyncTask
有人HttpPost
一個JSONObject
後。我正在填寫JSONObject
通過多個HashMaps
。傳遞的JSONObject到的AsyncTask然後使用HttpPost發送到PHP服務器
當我通過JSONObject
到AsyncTask
我得到了什麼看起來像JSONObject
引用,從這一點,我不確定如何解析正確,PHP服務器端。
JSONObject
構建器方法和AsyncTask
呼叫者:
HashMap<Integer, HashMap<String, String>> finalHash;
HashMap<String, String> semiFinalHash;
private void completeSurvey() throws JSONException {
finalHash = new HashMap<Integer, HashMap<String, String>>();
semiFinalHash = new HashMap<String, String>();
JSONArray jArray = new JSONArray();
JSONObject joMap = new JSONObject();
for (int indexInt = 0; indexInt <= lightingMap.size(); indexInt++) {
if (lightingMap.containsKey(indexInt) && !placeholderHashMap.containsKey(indexInt)) {
int checkId = indexInt;
placeholderHashMap.put(checkId, "null");
}
}
Log.i("", "" + placeholderHashMap.toString());
for (int finalOutPut = 0; finalOutPut < lightingMap.size(); finalOutPut++) {
JSONObject jo = new JSONObject();
try {
int id = finalOutPut + 1;
jo.put("id", id);
jo.put("SurveyPhoto", placeholderHashMap.get(id));
jo.put("Lighting", lightingMap.get(id));
jo.put("Notes", signSpecNotesMap.get(id));
jo.put("AdditionalService", chkBxMap.get(id));
//jo.put("Coordinates", latLngMap.get(id));
jArray.put(jo);
} catch (Exception e) {
Log.e("", "" + e.getMessage());
}
}
joMap.put(businessName, jArray);
Log.i("JSONObject", joMap.toString());
new CompleteSurveyAsync().execute(joMap);
}
AsyncTask
類別:
class CompleteSurveyAsync extends AsyncTask<JSONObject, Void, String> {
public ProgressDialog progressDialog = new ProgressDialog(Main.this);
protected void onPreExecute() {
progressDialog.setMessage("Submitting Data to Server...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface diaInterface) {
CompleteSurveyAsync.this.cancel(true);
diaInterface.dismiss();
}
});
}
String inString, parameterPass;
JSONObject jobject;
@Override
protected String doInBackground(JSONObject... jObject) {
parameterPass = jObject.toString();
Log.i("doInBackground", parameterPass);
String url_select = "http://www.somewebsite.com/db/completedSurvey.php";
HttpResponse response;
try {
HttpPost httpPost = new HttpPost(url_select);
HttpClient httpClient = new DefaultHttpClient();
httpPost.setEntity(new ByteArrayEntity(jObject.toString().getBytes("UTF8")));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
response = (HttpResponse) httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null) {
InputStream in = response.getEntity().getContent();
inString = in.toString();
Log.i("InputStream", "" + in.toString());
}
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
return parameterPass;
}
protected void onPostExecute(String s) {
this.progressDialog.dismiss();
Log.i("onPostExecute", s);
Toast.makeText(Main.this, s, Toast.LENGTH_LONG).show();
}
}
這使我的logcat的輸出:
11-13 09:52:45.606: I/JSONObject(2601): {"SOME COMPANY":[{"Notes":"null","SurveyPhoto":"[[email protected]","id":1,"Lighting":1,"AdditionalService":0},{"Notes":"null","SurveyPhoto":"null","id":2,"Lighting":0,"AdditionalService":0},{"Notes":"null","SurveyPhoto":"null","id":3,"Lighting":1,"AdditionalService":0},{"Notes":"null","SurveyPhoto":"null","id":4,"Lighting":1,"AdditionalService":0}]}
11-13 09:52:45.626: I/doInBackground(2601): [Lorg.json.JSONObject;@416ad478
11-13 09:52:45.786: I/InputStream(2601): [email protected]
11-13 09:52:45.816: I/onPostExecute(2601): [Lorg.json.JSONObject;@416ad478
我驗證了JSON
,構建它工作正常,但經過AsyncTask
當我開始變得[Lorg.json.JSONObject;@4158c990
,這是一個對象引用?如果是這樣,我該如何解析這個PHP服務器端?
我當前的PHP設置爲只寫入一個文件:
$json = file_get_contents('php://input');
$jsonObj = json_decode($json, true);
$jsonFile = "json.txt";
$fh = fopen($jsonFile, 'a');
fwrite($fh, $json);
fwrite($fh, "\n");
fwrite($fh, $jsonObj);
fwrite($fh, "\n");
fclose($fh);
輸出(json.txt):
[Lorg.json.JSONObject;@416ad478
你是一個生命的救星先生,大加讚賞。希望我能再多+1次。曾幾何時,我知道,應該退後一步,真正想到物體是如何通過的。再次感謝! – jnthnjns