DECOM presing一个字符串日食 [英] Decompresing a string in eclipse
问题描述
我碰到我的.NET Web服务一个COM pressed字符串。在Eclipse我想DECOM preSS的刺痛。
I'm getting a compressed string from my .net web service. In eclipse I want to decompress the sting.
的COM pressing字符串中的Web服务:
Compressing the string in web service:
public static string Compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}
在使用异步Android的Web服务响应:
Web Service response in android using async:
resultJsonString = new AsyncGetProductIdResults().execute(link).get();
String temp = unZip(resultJsonString);
异步:
class AsyncGetProductIdResults extends AsyncTask<String, Void, String>
{
@SuppressWarnings("null")
@Override
protected String doInBackground(String... params)
{
String url = params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
try
{
HttpResponse response = httpClient.execute(httpPost);
return EntityUtils.toString(response.getEntity());
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String idResult)
{
if (idResult == null)
public static byte[] ZipStr(string str)
{
{
return;
}
}
}
DECOM $ P $ android的PSS:
Decompress in android:
public String unZip(String zipped) throws DataFormatException, IOException
{
byte[] bytes = zipped.getBytes();
Inflater decompressed = new Inflater();
decompressed.setInput(bytes);
byte[] result = new byte[1000];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while (decompressed.inflate(result) != 0)
buffer.write(result);
decompressed.end();
return buffer.toString();
}
logcat的:
Logcat:
01-08 11:07:59.016: E/AndroidRuntime(1327): FATAL EXCEPTION: main
01-08 11:07:59.016: E/AndroidRuntime(1327): java.lang.IllegalStateException: Could not execute method of the activity
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.view.View$1.onClick(View.java:3591)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.view.View.performClick(View.java:4084)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.view.View$PerformClick.run(View.java:16966)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.os.Handler.handleCallback(Handler.java:615)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.os.Looper.loop(Looper.java:137)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 11:07:59.016: E/AndroidRuntime(1327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-08 11:07:59.016: E/AndroidRuntime(1327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 11:07:59.016: E/AndroidRuntime(1327): at dalvik.system.NativeStart.main(Native Method)
01-08 11:07:59.016: E/AndroidRuntime(1327): Caused by: java.lang.reflect.InvocationTargetException
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 11:07:59.016: E/AndroidRuntime(1327): at android.view.View$1.onClick(View.java:3586)
01-08 11:07:59.016: E/AndroidRuntime(1327): ... 11 more
01-08 11:07:59.016: E/AndroidRuntime(1327): Caused by: java.util.zip.DataFormatException: data error
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.util.zip.Inflater.inflateImpl(Native Method)
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.util.zip.Inflater.inflate(Inflater.java:228)
01-08 11:07:59.016: E/AndroidRuntime(1327): at java.util.zip.Inflater.inflate(Inflater.java:205)
01-08 11:07:59.016: E/AndroidRuntime(1327): at com.example.testp.SecondActivity.unZip(SecondActivity.java:184)
01-08 11:07:59.016: E/AndroidRuntime(1327): at com.example.testp.SecondActivity.JSONDataSync(SecondActivity.java:83)
01-08 11:07:59.016: E/AndroidRuntime(1327): ... 14 more
我的Web服务和响应工作之前,我开始实施COM pression和DECOM pression。任何帮助将AP preciated。即使你可以点我的材料我可以读了。
My web service and response worked before I started to implement the compression and decompression. Any help will be appreciated. Even if you can just point me to material I can read up on.
修改1
适用<提议更改后href="http://stackoverflow.com/a/21016711/1587302">http://stackoverflow.com/a/21016711/1587302
public static byte[] ZipStr(string str)
{
using (MemoryStream output = new MemoryStream())
{
using (DeflateStream gzip =
new DeflateStream(output, CompressionMode.Compress))
{
using (StreamWriter writer =
new StreamWriter(gzip, System.Text.Encoding.UTF8))
{
writer.Write(str);
}
}
return output.ToArray();
}
}
的AsyncTask在Android:
Asynctask on android:
class AsyncGetProductIdResultsBytes extends AsyncTask<String, Void, byte[]>
{
@SuppressWarnings("null")
@Override
protected byte[] doInBackground(String... params)
{
String url = params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
try
{
HttpResponse response = httpClient.execute(httpPost);
return EntityUtils.toByteArray(response.getEntity());
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] idResult)
{
if (idResult == null)
{
return;
}
}
}
DECOM $ P $ android系统PSS:还在忙着DECOM pression
Decompress in android: Still busy with decompression.
内容的web服务端后,COM pression:[1,97,0,0,31,139,...]
Content of byte[] on webservice side after compression: [1,97,0,0,31,139, ...]
字节的内容之前DECOM pression安卓方[]:[123,34,103,101,116,65,...]
Content of byte[] on android side before decompression: [123,34,103,101,116,65, ...]
只是想知道,如果做即时通讯的COM pression正确的web服务的一面呢?如果当其收到的byte []的变化的内容在Android上的一面呢?
Just wanting to know if im doing the compression right on webservice side? Should the content of the byte[] change when its received on android side?
推荐答案
我看到两个问题:
首先是要在编码数据的基础64在C#中,但你不能解码它在Java中,至少你已经张贴了code。
The first is that you are encoding the data in base 64 in c# but you are not decoding it in java, at least in the code that you have posted.
第二个是,你DECOM pressing一个字节[]
谁包含com $ P $的长度pssed数据和COM pressed数据,这是正常的,让一个错误。
The second is that you are decompressing a byte[]
who contains the length of the compressed data and the compressed data, which is normal that give an error.
在这里,你有你正在尝试做的一个例子:<一href="http://stackoverflow.com/a/18606270/1587302">http://stackoverflow.com/a/18606270/1587302
Here you have an example of that you are trying to do: http://stackoverflow.com/a/18606270/1587302
和那里,我不知道,如果可能导致问题的另一件事:在C#中的COM pressing与GZipStream,不使用zlib的谁;在java中DECOM pressing与充气,谁使用zlib的。正如我说我真的不知道,如果这是一个问题,但我建议使用相同的两侧:在C#<一href="http://msdn.microsoft.com/es-es/library/system.io.com$p$pssion.deflatestream%28v=vs.110%29.aspx"相对=nofollow> DeflateStream 使用zlib的和在java中的 GZIPInputStream 使用gzip压缩。 修改:作为user3074196确认是他的意见,这是一个问题,太
And there another thing that i am not sure that if can cause problems: in c# you are compressing with GZipStream, who does not use zlib; in java decompressing with Inflater , who use zlib. As i said i am not really sure if it is a problem, but i recommend use the same in both sides: in c# DeflateStream uses zlib and in java GZIPInputStream uses gzip. Edit: as user3074196 confirm is his comment this is a problem too.
回应编辑1
我觉得现在的问题是,你送它与协议HTTP的数据interfiere二进制数据。我能想到的两个解决方案:
I think that the problem is that you are sending binary data which interfiere with the data of protocol http. I can think two solutions:
首先是发送一个字符串连接codeD在基地64,如在C它,你第一个版本和去$ C $ Java编写的。
The first is send a string encoded in base 64, as in you first version and decode it in java.
二是使用流申请和传输二进制数据。
The second is use stream request and transmit binary data.
这篇关于DECOM presing一个字符串日食的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!