[問題] Java程式如何解壓縮WebServer傳來的gzip網頁內容?
其實這是Android內的程式,不過應該差不多
我是用apache的HttpClient做連線
WebServer有支援Gzip壓縮,所以我用
httpRequest.addHeader("Accept-Encoding", "deflate,gzip");
在Request時請求一個經Gzip壓縮後的結果
但因為我的APP不是Browser所以沒辦法把回傳的結果解壓縮
有查了一下GZIPInputStream的用法,但找不到直接把回傳結果解壓縮印出來的方法
感覺先把結果存成檔案再解壓縮似乎可以?
請問大家如果是你們會怎麼做?謝謝!
以下是部分Code
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
/* 宣告網址字串 */
String uriAPI = "http://mpc06.cs.nctu.edu.tw/lifestyleAPI/test2.php";
/* 建立HTTP Get連線 */
HttpGet httpRequest = new HttpGet(uriAPI);
httpRequest.addHeader("Accept-Encoding", "deflate,gzip");
try
{
/* 發出HTTP request */
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
/* 若狀態碼為200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出回應字串 */
String strResult = EntityUtils.toString(httpResponse.getEntity(),
HTTP.UTF_8);
/* 這邊的strResult是被Gzip壓縮過的亂碼字串 */
/* 刪除多餘字元 */
strResult = eregi_replace("(\r\n|\r|\n|\n\r)", "", strResult);
mTextView1.setText(strResult);
} else
{
mTextView1.setText("Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
--
☆
╭──╮╭──╮┌╭─╮ ○ ┌┐┌┐╭──╮┌┐┌┐ ○ ┌╭─╮╭──┐
│╭─┘│╭╮││╭╮│ ┌┐ │││││╭─╯│╰╭╯ ┌┐ │╭╮││╭╮│
│┌┐││┌─╯││││ ││ │╰┘│╰─╮╮│╭╮╮ ││ ││││╰─┐│
╰──╯╰──╯└┘└┘ └┘ ╰─└┘╰──╯└┘└┘ └┘ └┘└┘╰──╯
http://www.wretch.cc/blog/Geniusking ☆
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.198.25