如何在android中将JSONArray转换为Base64格式?

时间:2014-11-10 12:57:14

标签: android

您好我想将 JSONArray 转换为 Base64 格式,并希望将其发布到服务器上 我正在给一个arraylist作为json数组的参数,如此

  JsonArray jArray = new JSONArray(<<ArrayList NAME>>);


  [{previous_cost=No Previous Cost Available, id=B00PDBKATW, localCost=4.52, time=2014-12-10            17:22:08, totalCost=39.51, Price=$32.52, Shipping=$6.99, upc_code=603084267415, Product_name=(3 Piece Bundle) Garnier Fructis Sleek & Shine Shampoo  Conditioner and Anti-Frizz Serum  With Argan Oil from Morocco}, {previous_cost=$19.99, id=B0029PQ2MY, localCost=10, time=2014-12-10 17:59:21, totalCost=26.98, Price=$19.99, Shipping=$6.99, upc_code=041116006258, Product_name=24 each: Topps Ring Pop (625)}, {previous_cost=$64.98, id=B009S28ON6, localCost=10, time=2014-12-10 17:59:20, totalCost=71.13, Price=$64.98, Shipping=$6.15, upc_code=041116006258, Product_name=Bazooka Ring Pop Box  0.50 Ounce (Pack of 24)}, {previous_cost=No Previous Cost Available, id=B00CUGBH5Y, localCost=4.52, time=2014-12-10 17:22:09, totalCost=37.10, Price=$37.10, Shipping=No Shipping, upc_code=603084267415, Product_name=FRUCTIS SLEEKand SHINE COND 13OZ}, {previous_cost=No Previous Cost Available, id=B005GQ6F4I, localCost=4.52, time=2014-12-10 17:22:08, totalCost=9.83, Price=$4.58, Shipping=No Shipping, upc_code=603084267415, Product_name=Garnier Conditioner  Sleek and Shine  13 Fluid Ounce}, {previous_cost=No Previous Cost Available, id=B00OJC2QCU, localCost=4.52, time=2014-12-10 17:22:09, totalCost=40.99, Price=$40.99, Shipping=No Shipping, upc_code=603084267415, Product_name=Garnier Conditioner  Sleek and Shine  13 Fluid Ounce (4 Pack)}, {previous_cost=No Previous Cost Available, id=B00NFUJM1K, localCost=4.52, time=2014-12-10 17:22:08, totalCost=20.99, Price=$20.99, Shipping=No Shipping, upc_code=603084267415, Product_name=Garnier Fructis Conditioner Sleek & Shine 13oz Frizzy/Dry (2 Pack)}, {previous_cost=No Previous Cost Available, id=B00GMP7VZE, localCost=4.52, time=2014-12-10 17:22:09, totalCost=30.99, Price=$30.99, Shipping=No Shipping, upc_code=603084267415, Product_name=Garnier Fructis Conditioner Sleek & Shine 13oz Frizzy/Dry (3 Pack)}, {previous_cost=No Previous Cost Available, id=B00G4EPV78, localCost=4.52, time=2014-12-10 17:22:10, totalCost=61.99, Price=$61.99, Shipping=No Shipping, upc_code=603084267415, Product_name=Garnier Fructis Conditioner Sleek & Shine 13oz Frizzy/Dry (6 Pack)}, {previous_cost=$21.62, id=B00FFJ0AEM, localCost=12, time=2014-12-10 17:54:53, totalCost=27.61, Price=$21.62, Shipping=$5.99, upc_code=037000867616, Product_name=Old Spice Swagger Body Wash - Man Sized - Convenient Pump Bottle - 32 Fluid Ounces}, {previous_cost=No Previous Cost Available, id=B00EA8DU1O, localCost=12, time=2014-12-10 17:35:51, totalCost=0.00, Price=0,00, Shipping=No Shipping, upc_code=080878173228, Product_name=Pantene Pro-V Anti-Breakage Shampoo  22.8 Fluid Ounce}, {previous_cost=$19.99, id=B00D97KV14, localCost=10, time=2014-12-10 17:59:21, totalCost=25.98, Price=$19.99, Shipping=$5.99, upc_code=041116006258, Product_name=RING POP ORIG .5 OUNCES 24 COUNT}, {previous_cost=$19.95, id=B0052Y49PG, localCost=10, time=2014-12-10 17:59:20, totalCost=25.44, Price=$19.95, Shipping=$5.49, upc_code=041116006258, Product_name=Ring Pop Fruit Fest (24 pack)}, {previous_cost=No Previous Cost Available, id=B0087W58DW, localCost=10, time=2014-12-10 17:55:23, totalCost=47.78, Price=$47.78, Shipping=No Shipping, upc_code=074182290948, Product_name=Softsoap Elements Milk Protein & Honey Moisturizing Hand Soap 7.5 oz (Pack of 12)}, {previous_cost=No Previous Cost Available, id=B00CQ561EG, localCost=10, time=2014-12-10 17:55:24, totalCost=14.59, Price=$14.59, Shipping=No Shipping, upc_code=074182290948, Product_name=Softsoap Elements Milk Protein and Honey Moisturizing Hand Soap 7.5 oz (Pack of 2)}, {previous_cost=No Previous Cost Available, id=B00KOBQ78Y, localCost=10, time=2014-12-10 17:55:24, totalCost=200.24, Price=$200.24, Shipping=No Shipping, upc_code=074182290948, Product_name=Softsoap Moisturizing Hand Soap Milk & Golden Honey}]

我试过这个,

  String Encoded = new String(Base64.decode(jArray.toString(), Base64.DEFAULT));

以及在服务器上发布jsonarray的代码,

 DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(<<URL WHERE DATA IS POSTING>>);
        try{
            String Encoded = Base64.encodeToString((jArray.toString()).getBytes(), Base64.DEFAULT);
            String param = Encoded;
            sBuilder = new StringBuilder();
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("var1", param));
            Log.d("Params", ""+list);
            Log.d("Encoded Format", ""+Encoded);

            decoded = Base64.decode(param, Base64.DEFAULT);
            Log.d("decoded byte Format", ""+Encoded);
            Log.d("Decoded Format", ""+Decoded);
            httpPost.setEntity(new UrlEncodedFormEntity(list));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.d("HttpResponse", ""+httpResponse);
            StatusLine statusLine = httpResponse.getStatusLine();
            int i = statusLine.getStatusCode();
            if(i == 200){

                BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
                String data = reader.readLine();

                sBuilder.append(data);

            }
            Log.d("Getting JSON","JSON data DownLoad"+ "\n" + sBuilder);
            isMailSent = true;
        }catch(Exception e){


        }

并且作为结果我得到了编码的字符串日志,例如: -

12-11 16:06:46.785: D/Encoded Format(6514): WyJ7cHJldmlvdXNfY29zdD1ObyBQcmV2aW91cyBDb3N0IEF2YWlsYWJsZSwgaWQ9QjAwUERCS0FU
12-11 16:06:46.785: D/Encoded Format(6514): VywgbG9jYWxDb3N0PTQuNTIsIHRpbWU9MjAxNC0xMi0xMCAxNzoyMjowOCwgdG90YWxDb3N0PTM5
12-11 16:06:46.785: D/Encoded Format(6514): LjUxLCBQcmljZT0kMzIuNTIsIFNoaXBwaW5nPSQ2Ljk5LCB1cGNfY29kZT02MDMwODQyNjc0MTUs
12-11 16:06:46.785: D/Encoded Format(6514): IFByb2R1Y3RfbmFtZT0oMyBQaWVjZSBCdW5kbGUpIEdhcm5pZXIgRnJ1Y3RpcyBTbGVlayAmIFNo

等等。

我希望这些编码的字符串成为一个字符串,但它会编码成部分。 谢谢!

1 个答案:

答案 0 :(得分:0)

方法Base63.decode()用于解码,即 base64 - &gt;表示JSONArray的字符串 你想要逆向转换。请改用Base64.encode方法