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
答案 0 :(得分:0)
以下是一种可以帮助您的方法:
public static final String URL_CONNECTION = "https://your-url.com";
public static final String CONTENT_TYPE_FIELD = "Content-Type";
public static final String CONTENT_TYPE = "application/json";
public static final String ACCEPT_FIELD = "Accept";
public static final String ACCEPT = "application/json";
public static final String AUTHORIZATION_FIELD = "Authorization";
private String createUser() throws JSONException, ClientProtocolException,
IOException, UnprocessableEntityException {
//Result of the server
String result = null;
//Objects to connect to server
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
//URL t coonnect to server
String url = HTTPHelper.URL_CONNECTION + PATH;
HttpPost httpPost = new HttpPost(url);
//Some necessary parameters that are needed in most of servers
httpPost.addHeader(HTTPHelper.CONTENT_TYPE_FIELD,
HTTPHelper.CONTENT_TYPE);
httpPost.addHeader(HTTPHelper.ACCEPT_FIELD, HTTPHelper.ACCEPT);
httpPost.setEntity(new StringEntity(sendUser().toString(), HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity entity = response.getEntity();
result = HTTPHelper.getASCIIContentFromEntity(entity);
return result;
}
在上面的示例中,当我正在进行httpPost.setEntity(new StringEntity(sendUser().toString(), HTTP.UTF_8));
时,我正在将JSON对象(sendUser()
返回JSON)转换为String以发送到服务器。