public JSONArray getProductCartJSON() {
JSONArray product_array = new JSONArray();
try {
for (FoodItem item : getItem_list()) {
JSONObject object = new JSONObject();
object.put("id", "" + item.getId());
object.put("quantity", "" + item.getCart());
object.put("note", "");
product_array.put(object);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return product_array;
}
这里正在调用上述方法发送到服务器
public static HttpResponse posthttp(String url2) {
HttpResponse httpResponse = null;
JSONArray product_array = null;
URL url;
try {
url = new URL(url1);
Log.e(TAG, "url1 in posthttp" + url);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(URLEncoder.encode("005", "UTF-8"));
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setRequestProperty("Content-Type","application/json");
urlConnection.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("t", "t");
jsonParam.put("m", "m");
jsonParam.put("n", "n");
jsonParam.put("a", "a");
jsonParam.put("p", "p");
jsonParam.put("e", "e");
product_array = new JSONArray();
product_array = CartHandler.getCartHandler().getProductCartJSON();
jsonParam.put("pj", product_array.toString());
OutputStreamWriter out = new OutputStreamWriter(
urlConnection.getOutputStream());
out.write(jsonParam.toString());
out.close();
InputStream is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = br.readLine()) != null) {
response.append(line);
response.append('\n');
}
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println(response.toString());
} else {
System.out.println("Incorrect response code");
}
br.close();
urlConnection.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return httpResponse;
}
我发送 JSONObjects 没有问题,但 JSONArray 无法发送,它甚至没有显示任何错误,我正在获取所有 JSONObjects 的详细信息,但不是 JSONArray