1 import java.io.ByteArrayOutputStream;
2 import java.io.File;
3 import java.io.FileOutputStream;
4 import java.io.InputStream;
5 import java.net.HttpURLConnection;
6 import java.net.URL;
7
8 public class NetTool {
9
10 /**
11 * 获得url代码数据
12 * */
13 public static String getHtml(String path, String encoding) throws Exception {
14 URL url = new URL(path);
15 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
16 conn.setRequestMethod("GET");
17 conn.setConnectTimeout(6 * 1000);
18 System.out.println(conn.getResponseCode());
19 if (conn.getResponseCode() == 200) {
20 InputStream inputStream = conn.getInputStream();
21 byte[] data = readStream(inputStream);
22 return new String(data, encoding);
23 }
24 return null;
25 }
26
27 /**
28 * 获取指定路径,的数据。
29 * **/
30 public static byte[] getImage(String urlpath) throws Exception {
31 URL url = new URL(urlpath);
32 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
33 conn.setRequestMethod("GET");
34 conn.setConnectTimeout(6 * 1000);
35 if (conn.getResponseCode() == 200) {
36 InputStream inputStream = conn.getInputStream();
37 return readStream(inputStream);
38 }
39 return null;
40 }
41
42 /**
43 * 读取数据 输入流
44 * */
45 public static byte[] readStream(InputStream inStream) throws Exception {
46 ByteArrayOutputStream outstream = new ByteArrayOutputStream();
47 byte[] buffer = new byte[1024];
48 int len = -1;
49 while ((len = inStream.read(buffer)) != -1) {
50 outstream.write(buffer, 0, len);
51 }
52 outstream.close();
53 inStream.close();
54 return outstream.toByteArray();
55 }
56 }

 1 editText.setText("http://apps.hi.baidu.com/share/detail/30420431");
2
3 button.setOnClickListener(new View.OnClickListener() {
4 @Override
5 public void onClick(View v) {
6 String strHtmlUrl = editText.getText().toString();
7 try {
8 html = NetTool.getHtml(strHtmlUrl, "GB2312");
9 textView.setText(html);
10 } catch (Exception e) {
11 Log.i("Activity02", e.toString());
12 Toast.makeText(Activity02.this, "获得网页失败", 1).show();
13 }
14
15 }
16 });

PS:有些网站 弄下来之后是中文是乱码, why?

我一遇到编码问题就头晕 @@@

String dataString = "";
String urlString
= "http://www.qidian.com/BookReader/1995036.aspx";
try {
URL url
= new URL(urlString);
HttpURLConnection conn
= (HttpURLConnection) url.openConnection();

InputStreamReader
in = new InputStreamReader(conn.getInputStream(),"GB2312");

BufferedReader buffer
= new BufferedReader(in);
String inputString
= "";
while ((inputString = buffer.readLine()) != null) {
dataString
+= inputString + "\n";
}
in.close();
conn.disconnect();
}
catch (Exception e) {
System.
out.println(e.getMessage() + "");
}
setContentView(R.layout.main);
TextView tv
= (TextView) this.findViewById(R.id.textView);
//dataString = FormatStr(dataString);
tv.setText(dataString);

InputStreamReader in = new InputStreamReader(conn.getInputStream(),"GB2312"); 通过这样重新编码后 显示出来这个网页中文没有乱码

但是 当我把urlString 换成百度的时候 又有乱码了············坑爹 百度的是要用UTF-8 才不会有中文乱码!!

PS:跪求一劳永逸的解决方案,有没有一种方法能够获得网站的网页编码格式啊······················     


posted on 2011-08-15 16:01  QZB  阅读(577)  评论(1编辑  收藏  举报