在我的Android购物车应用程序中将JSON数组发送到服务器

时间:2015-06-24 07:43:15

标签: android arrays json web-services

我想将数据库中的购物车列表发送到服务器。但我不知道如何将JSON数组发送到服务器。

我想发送如下内容。

http://xxx./cart_total/
{"lat":"23.82","long":"72.08","deviceID":"AFDSGHGGSJGSJJ","date":"22-06-2015","cartItems":
[{"ProductID":"1","ProductQuantity":"3"},  
 {"ProductID":"2","ProductQuantity":"5"}, 
 {"ProductID":"6","ProductQuantity":"4"}]}

在普通的json中,我这样经过。

    public boolean isAuthenticated1(final String serialnumber) 
    {
        boolean isAuthorized = false;
         String encoder1 = "";
         String url="http://mywebsite.com/cart_total/"+serialnumber;

        try {

            if (isNetworkConnected()) {

                AuthenticationResultJSONObject = new JSONObject(doFetchDataFromWebService_Simple_OnlyJSONResponse(url));    

                Log.v("Online", "User json array    ===  "+AuthenticationResultJSONObject);  

                }else{
                    Toast.makeText(Card_listview.this, "Please check your internet connection and try again.", 100).show();

                }
            }
            catch(Exception e){ 
            }
            finally{
                if(AuthenticationResultJSONObject!=null){
                    isAuthorized = true;
                }
                else
                {
                    isAuthorized=false;
                }
            }

        return isAuthorized;
        }

// Checking the internet connection
        public boolean isNetworkConnected() {
            ConnectivityManager connectivitymanagar = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkinfo = connectivitymanagar.getActiveNetworkInfo();
            if (networkinfo == null || !networkinfo.isConnectedOrConnecting()) {
                return false;
            } else {
                return true;
            }
        }

public String doFetchDataFromWebService_Simple_OnlyJSONResponse(String WebServiceURL) {
    String responseBody = "";
    JSONObject jobj = new JSONObject();
    try {
        HttpGet WSHttpPost = null;
        HttpClient WSHttpClient = null;
        HttpResponse WSHttpResponse = null;

        WSHttpClient = getNewHttpClient();
        WSHttpPost = new HttpGet(WebServiceURL);
        WSHttpResponse = WSHttpClient.execute(WSHttpPost);
        responseBody = EntityUtils.toString(WSHttpResponse.getEntity(), "UTF-8");

        Log.v("response1", "" + responseBody);

    } catch (Exception e) {
        Log.e("Exception ==> ", e.toString());
    }
    if (responseBody == null || responseBody.equalsIgnoreCase("null")) {
        return "";
    } else {
        return responseBody;
    }
}

public HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore
                .getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore)
            throws NoSuchAlgorithmException, KeyManagementException,
            KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port,
            boolean autoClose) throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host,
                port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}

那么我应该把json放在哪里,请帮助我。

感谢。

1 个答案:

答案 0 :(得分:4)

试试这个:

  

您必须将值POST到服务器,如下面的格式

private String getItems() 
{
    JSONObject dataObj = new JSONObject();
    try
    {
        dataObj.putOpt("lat", "");
        dataObj.putOpt("long","");
        dataObj.putOpt("deviceID", "");
        dataObj.putOpt( "date", "");

        JSONArray cartItemsArray = new JSONArray();
        JSONObject cartItemsObjedct;
        for (int i = 0; i < cartItems.size(); i++) 
        {
            cartItemsObjedct = new JSONObject();
            cartItemsObjedct.putOpt("ProductID", cartItems.get(i)
                    .getId);
            cartItemsObjedct.putOpt("ProductQuantity",cartItems.get(i).getProductQuantity);
            cartItemsArray.put(cartItemsObjedct);
        }

        dataObj.put("cartItems", cartItemsArray);

    } catch (JSONException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return dataObj.toString();
}