0
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

4

1 回答 1

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)转换为一个字符串以发送到服务器。

于 2013-08-06T06:29:09.323 回答