मैं एंड्रॉइड के लिए एक पायथन एप्लिकेशन पोर्ट कर रहा हूं और, किसी बिंदु पर, इस एप्लिकेशन को एक संपीड़ित डेटा भेजकर, एक वेब सेवा के साथ संवाद करना है।जावा (एंड्रॉइड) पर पाइथन और Deflater.deflate पर zlib.compress संगत है?
ताकि इसे अगली विधि का उपयोग करता है करने के लिए:
def stuff(self, data):
"Convert into UTF-8 and compress."
return zlib.compress(simplejson.dumps(data))
मैं अगली विधि का उपयोग कर रहा एंड्रॉयड में इस व्यवहार का अनुकरण करने की कोशिश करने के लिए:
private String compressString(String stringToCompress)
{
Log.i(TAG, "Compressing String " + stringToCompress);
byte[] input = stringToCompress.getBytes();
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
//compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// You cannot use an array that's the same size as the orginal because
// there is no guarantee that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished())
{
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.close();
} catch (IOException e)
{
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
Log.i(TAG, "Finished to compress string " + stringToCompress);
return new String(compressedData);
}
लेकिन से HTTP प्रतिसाद सर्वर सही नहीं है और मुझे लगता है कि ऐसा इसलिए है क्योंकि जावा में संपीड़न का परिणाम पायथन में एक जैसा नहीं है।
मैंने zlib.compress और डिफ्लेट दोनों के साथ "ए" को संपीड़ित करने वाला एक छोटा परीक्षण चलाया।
अजगर, zlib.compress() -> x% 9CSJT% 02% 00% 01M% 00% ए 6
एंड्रॉयड, Deflater.deflate -> एच% एफई% BF% BDK% 04% 00% 00b % 00b
मुझे Python में zlib.compress() के समान मान प्राप्त करने के लिए एंड्रॉइड में डेटा को कैसे संपीड़ित करना चाहिए?
कोई भी मदद, मार्गदर्शन या सूचक की बहुत सराहना की जाती है!
लाइन 'नई स्ट्रिंग (संपीड़ित डेटा) लौटाएं,' एक बग है। आप इस तरह स्ट्रिंग का उपयोग नहीं कर सकते हैं। –