Re: [android-developers] what's wrong with this http post to server code?

2011-06-26 Thread 陈彧堃
i found the problem by myself. It's problem of the usage of deflater,
correct solution should be:
public static byte[] deflaterCompress(String str) throws IOException
{
TOTAL_LEN = 0;
byte[] input = str.getBytes(encoding);
Deflater deflater = new Deflater();
deflater.setInput(input);
deflater.finish();
 byte[] buf = new byte[8192];
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 while (!deflater.finished()) {
 int byteCount = deflater.deflate(buf);
 TOTAL_LEN += byteCount;
 baos.write(buf, 0, byteCount);
 }
 deflater.end();

 byte[] compressedBytes = baos.toByteArray();
 return compressedBytes;
}

On Mon, Jun 27, 2011 at 1:09 PM, lily  wrote:

> **
> Have you add the internet permission in manifest like this?
>  android:name="android.permission.INTERNET">
>
> 于 2011/6/27 11:10, 陈彧堃 写道:
>
> I use deflater to compress string data. It works fine on java console
> project, but while running on android enviroment, server will report "buffer
> error" problem and reture 500.
>
>  SendMessage(String content, String url)
> {
>  byte[] bs = deflaterCompress(content);
>   URL url = new URL(urlStr);
> HttpURLConnection httpConn = (HttpURLConnection)
> url.openConnection();
> httpConn.setDoOutput(true);
> httpConn.setRequestMethod("POST");
> httpConn.setDoInput(true);
>
> //httpConn.setRequestProperty("Content-length",
> String.valueOf(UmengDeviceHelper.TOTAL_LEN));
> httpConn.setRequestProperty("Content-Type",
> "application/octet-stream");
> httpConn.setRequestProperty("Content-Encoding", "deflate");
> httpConn.connect();
>  DataOutputStream outputStream = new
> DataOutputStream(httpConn.getOutputStream());
> outputStream.write(bs);
> //outputStream.write(content.getBytes("utf-8"));
> outputStream.flush();
> outputStream.close();
>
> int responseCode = 500;
> try {
> responseCode = httpConn.getResponseCode();
> Log.i("MobclickAgent", "reture code is:" + responseCode);
> } catch (NumberFormatException e) {
> // ignore
>  e.printStackTrace();
> }
> }
>
>  public static byte[] deflaterCompress(String str) throws IOException
>  {
>  byte[] input = str.getBytes(encoding);
>  byte[] output = new byte[100];
>  Deflater compresser = new Deflater();
>  compresser.setInput(input);
>  compresser.finish();
>  TOTAL_LEN = compresser.deflate(output);
>   return output;
>  }
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] what's wrong with this http post to server code?

2011-06-26 Thread lily

Have you add the internet permission in manifest like this?
android:name="android.permission.INTERNET">


? 2011/6/27 11:10, ??? ??:
I use deflater to compress string data. It works fine on java console 
project, but while running on android enviroment, server will report 
"buffer error" problem and reture 500.


SendMessage(String content, String url)
{
byte[] bs = deflaterCompress(content);
URL url = new URL(urlStr);
   HttpURLConnection httpConn = (HttpURLConnection) 
url.openConnection();

   httpConn.setDoOutput(true);
   httpConn.setRequestMethod("POST");
   httpConn.setDoInput(true);
   //httpConn.setRequestProperty("Content-length", 
String.valueOf(UmengDeviceHelper.TOTAL_LEN));
   httpConn.setRequestProperty("Content-Type", 
"application/octet-stream");

   httpConn.setRequestProperty("Content-Encoding", "deflate");
   httpConn.connect();
   DataOutputStream outputStream = new 
DataOutputStream(httpConn.getOutputStream());

   outputStream.write(bs);
   //outputStream.write(content.getBytes("utf-8"));
   outputStream.flush();
   outputStream.close();
   int responseCode = 500;
   try {
   responseCode = httpConn.getResponseCode();
   Log.i("MobclickAgent", "reture code is:" + responseCode);
   } catch (NumberFormatException e) {
   // ignore
e.printStackTrace();
   }
}

public static byte[] deflaterCompress(String str) throws IOException
{
byte[] input = str.getBytes(encoding);
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
TOTAL_LEN = compresser.deflate(output);
return output;
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en 


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] what's wrong with this http post to server code?

2011-06-26 Thread 陈彧堃
i tried that, result has no difference

2011/6/27 Дениска, Рынский (из за Угла) 

> You do not set content length. It supposes to be chunked, but it's worth to
> set some http watcher and see what actually you transfer.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] what's wrong with this http post to server code?

2011-06-26 Thread из за Угла
You do not set content length. It supposes to be chunked, but it's worth to 
set some http watcher and see what actually you transfer.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] what's wrong with this http post to server code?

2011-06-26 Thread 陈彧堃
I use deflater to compress string data. It works fine on java console
project, but while running on android enviroment, server will report "buffer
error" problem and reture 500.

SendMessage(String content, String url)
{
byte[] bs = deflaterCompress(content);
 URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection)
url.openConnection();
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setDoInput(true);

//httpConn.setRequestProperty("Content-length",
String.valueOf(UmengDeviceHelper.TOTAL_LEN));
httpConn.setRequestProperty("Content-Type",
"application/octet-stream");
httpConn.setRequestProperty("Content-Encoding", "deflate");
httpConn.connect();
DataOutputStream outputStream = new
DataOutputStream(httpConn.getOutputStream());
outputStream.write(bs);
//outputStream.write(content.getBytes("utf-8"));
outputStream.flush();
outputStream.close();

int responseCode = 500;
try {
responseCode = httpConn.getResponseCode();
Log.i("MobclickAgent", "reture code is:" + responseCode);
} catch (NumberFormatException e) {
// ignore
 e.printStackTrace();
}
}

public static byte[] deflaterCompress(String str) throws IOException
{
byte[] input = str.getBytes(encoding);
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
TOTAL_LEN = compresser.deflate(output);
 return output;
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en