论坛首页 Java版 J2ME

N6600访问HTTP

浏览 124 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-04-25
才接触J2ME,写一个访问网页的小程序,
查找了多方的资料,我下面使用的连接都是正确的,在模拟器上运行也正常,为什么到了真机上就返回
symbianos error = - 5120错误,

public static String getHTTPInfo() {
		String re = "";
		HttpConnection hc = null;
		DataInputStream dis = null;
		int rc; // as httpconnection error type
		StringBuffer messagebuffer = new StringBuffer();

		try {
			hc = (HttpConnection) Connector.open("http://www.google.cn/");
			hc.setRequestMethod(HttpConnection.POST);
			hc.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT");
			hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
			rc = hc.getResponseCode();

			if (rc != HttpConnection.HTTP_OK) {
				return "网络故障,错误码:" + rc;
			}

			dis = new DataInputStream(hc.openDataInputStream());
			long len; // 返回内容长度
			int ch; //   
			len = hc.getLength();

			if (len != -1) {
				// 内容太长,会引起内存溢出,所以最多只取前50个字符
				if (len <= 50) {
					for (int i = 0; i < len; i++) {
						if ((ch = dis.read()) != -1)
							messagebuffer.append((char) ch);
					}
				} else {
					for (int i = 0; i < 50; i++) {
						if ((ch = dis.read()) != -1)
							messagebuffer.append((char) ch);
					}
				}

				re = togb(messagebuffer.toString());
			} else {
				int j = 0;
				while ((ch = dis.read()) != -1) {
					// 内容太长,会引起内存溢出,所以最多只取前50个字符
					if (j >= 50) {
						break;
					}
					messagebuffer.append((char) ch);
					j++;
				}

				re = togb(messagebuffer.toString());
			}
			dis.close();

			return re;
		} catch (Exception e) {
			return "错误:" + e.getMessage();
		} finally {
			if (hc != null)
				try {
					hc.close();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}

			if (dis != null)
				try {
					dis.close();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}

		}
	}
   
论坛首页 Java版 J2ME

跳转论坛:
JavaEye推荐
    快速回复 引用上一条消息 (Alt+S)