Android解析socket或http流中文编码问题
2010-04-28 14:45
387 查看
这里直接拿了HTTP流实验了下
复制到剪贴板 Java代码
public String getHttpContent(String htmlUrl) throws IOException,
InterruptedException {
URL url;
InputStream is = null;
HttpURLConnection urlConn = null;
int count = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
url = new URL(htmlUrl);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(20000);
urlConn.setReadTimeout(20000);
is = urlConn.getInputStream();
byte[] buf = new byte[512];
int ch = -1;
while ((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count = count + ch;
}
} catch (final MalformedURLException me) {
me.getMessage();
throw me;
} catch (final IOException e) {
e.printStackTrace();
throw e;
}
return new String(baos.toByteArray(), "GB2312");
}
其实上面的方法很简单,刚开始那哥们用的BufferedReader去读,这样直接读出来String有问题,解码不对,后来自己读到 byteoutputstream里,然后读出字节自己手工编码就对了,可是昨天晚上发现了一个更简单的方法,我们真是走了一个大大的弯路,如下:
Java代码
public String getHttpContent(String htmlurl) throws Exception{
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(htmlUrl);
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(rp.getEntity()).trim();
}else{
return null;
}
}
apache的这些类用起来还真是方便,以后还要多多学习。
复制到剪贴板 Java代码
public String getHttpContent(String htmlUrl) throws IOException,
InterruptedException {
URL url;
InputStream is = null;
HttpURLConnection urlConn = null;
int count = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
url = new URL(htmlUrl);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(20000);
urlConn.setReadTimeout(20000);
is = urlConn.getInputStream();
byte[] buf = new byte[512];
int ch = -1;
while ((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count = count + ch;
}
} catch (final MalformedURLException me) {
me.getMessage();
throw me;
} catch (final IOException e) {
e.printStackTrace();
throw e;
}
return new String(baos.toByteArray(), "GB2312");
}
其实上面的方法很简单,刚开始那哥们用的BufferedReader去读,这样直接读出来String有问题,解码不对,后来自己读到 byteoutputstream里,然后读出字节自己手工编码就对了,可是昨天晚上发现了一个更简单的方法,我们真是走了一个大大的弯路,如下:
Java代码
public String getHttpContent(String htmlurl) throws Exception{
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(htmlUrl);
HttpResponse rp = hc.execute(get);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(rp.getEntity()).trim();
}else{
return null;
}
}
apache的这些类用起来还真是方便,以后还要多多学习。
相关文章推荐
- Android使用HttpUrlConnection get请求带中文参数的编码问题
- android sax 解析xml中文编码问题
- android URL 中文解析问题
- 【《深入解析Java Web技术内幕》学习思维导图】第3章 深入分析Java Web中的中文编码问题
- ASIHttpRequest 使用过程中,中文编码的问题
- Androidhttp中文乱码问题
- Android http中文乱码问题
- Android socket通信,读去中文汉字乱码问题解决。
- as3中的socket的readUTFBytes方法,它的UTF指的是UTF-8,只要服务器采用UTF-8编码,传输和修改中文的问题就能解决
- socket和HTTP在Android中的连接请求问题
- android 微信支付当body标签为中文时出现编码错误的问题
- HttpClient POST的中文编码问题
- android解析xml中文乱码问题
- HttpClient POST的中文编码问题
- C# HttpRequest 中文编码问题
- 从Android使用JSON传递参数到后台中文编码问题
- python中文编码问题(转自http://wolfmaster.iteye.com/blog/638029)
- Android InputStreamReader 解析gbk、gb2312编码的xml文件 编码问题.
- 解析php开发中的中文编码问题
- 解析php开发中的中文编码问题