Sunday, June 26, 2011

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

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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate