2013年9月24日星期二

http request using GZIP, but was unsuccessful , Jiqiu you ~ ~ ! !

Without GZIP, no problem at all , using GZIP on the problem , described as follows:
Click the button to send data to the server , then the server receives the data returned
Click the button every time the server receives data failed , a successful one failed , very depressed ! Engage in a day, and ask for help ah ~ ~

Android side code :
	public static String send(String url, String content, String contentType) throws Exception {
//建立连接
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
if (contentType != null) {
conn.setRequestProperty("content-type", contentType);
} else {
conn.setRequestProperty("content-type", CONTENT_TYPE_HTML);
}
conn.setConnectTimeout(10000);
conn.setReadTimeout(60000);
conn.setRequestMethod("POST");
conn.connect();

//发送数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(conn.getOutputStream()), "GBK"));
bw.write(content);
bw.flush();
bw.close();


//接收返回数据
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "GBK"));
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();

conn.disconnect();
return sb.toString();
}


server code :
	/**
 * 接收HTTP数据
 * @param request
 * @param response
 * @return 数据
 */
public static String receive(HttpServletRequest request) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(request.getInputStream()), "GBK"));
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br!=null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

/**
 * 响应HTTP数据
 * @param responseText
 * @param response
 */
public static void response(String responseText, HttpServletResponse response) {
BufferedWriter bw = null;
try {
response.setHeader("Content-Encoding", "gzip");
bw = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(response.getOutputStream()), "GBK"));
bw.write(responseText);
bw.flush();
bw.close();

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw!=null) bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


I wrote these two tools, you can try good intentions , Android build button on the line , client services Servlet build on the line .

so dotted , thank you ! ! !
------ Solution ---------------------------------------- ----
seemingly GZIP the JDK itself is a problem , you look at this article useful to you : http://cin-ie.iteye.com/blog/859822
- ----- For reference only ---------------------------------------
By the way , the exception is : java.io.IOException: Unknown format
on the Android side :
BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "GBK"));

thrown out
------ For reference only ------------------------------- --------
back to the 2nd floor , try it today and see as not
------ For reference only --------------- ------------------------
really is such a thing , put like that
------ For reference only ---------------------------------------
put is not so , hey

没有评论:

发表评论