Iniciando no SuperWaba

2 respostas
fabianofrizzo

Bom dia Galera to começando a mexer com o SuperWaba agora…

E parti para a parte de conexão… Copiei a classe httpConnection que é disponibilizada no próprio site do superwaba só que ela esta dando uns erros e eu não consigo identificar quais são os erros será que alguém poderia me dar uma luz…

Abaixo segue o código da classe httpConnection

package connection;

import waba.io.DataStream;
import waba.io.Socket;
import waba.sys.Convert;
import waba.util.Hashtable;
import waba.util.Vector;

public class httpConnection

{

	private waba.io.Socket sock = null;
	private String url = null;
	private String protocol = null;
	private String host = null;
	private String uri = null;
	private int xport = 80;
	private DataStream ds = null;
	private Hashtable headers = new Hashtable(10);
	private Hashtable parameters = new Hashtable(10);
	private Hashtable content_headers = new Hashtable(10);
	private byte[] buffer = null;
	private boolean read_headers = false;


	private String toHex(int num)

	{

		String tmp = Convert.toString((long) num, 16);
		tmp = "00" + tmp;
		tmp = tmp.substring(tmp.length() - 2);
		tmp = "%" + tmp;
		return tmp;

	}

	public httpConnection(String url_string, int port, int bufsize)
			throws Exception	{
		int pos, pos1;

		// comment out on device
		// waba.applet.JavaBridge.setNonGUIApp();

		xport = port;
		url = url_string;

		pos = url_string.indexOf("http://");

		if (pos != -1) {
			protocol = url_string.substring(pos, 4);
		}

		pos1 = url_string.indexOf("/", pos + 7);

		if (pos1 == -1) {
			throw new Exception("MalFormedURL ");
		}

		if (pos != -1) {
			host = url_string.substring(pos + 7, pos1);
		} else {
			host = url_string.substring(0, pos1);
		}

		uri = url_string.substring(pos1 + 1);
		buffer = new byte[bufsize];

	}


	public String URLencode(String s) {
		StringBuffer sbuf = new StringBuffer();
		int len = s.length();
		for (int i = 0; i < len; i++) {
			int ch = s.charAt(i);
			if ('A' <= ch && ch <= 'Z') { // 'A'..'Z'
				sbuf.append((char) ch);
			} else if ('a' <= ch && ch <= 'z') { // 'a'..'z'
				sbuf.append((char) ch);
			} else if ('0' <= ch && ch <= '9') { // '0'..'9'
				sbuf.append((char) ch);
			} else if (ch == ' ') { // space
				sbuf.append('+');
			} else if (ch == '-'
					|| ch == '_' // unreserved
					|| ch == '.' || ch == '!' || ch == '~' || ch == '*'
					|| ch == '\'' || ch == '(' || ch == ')') {
				sbuf.append((char) ch);
			} else if (ch <= 0x007f) { // other ASCII
				sbuf.append(toHex(ch));
			} else if (ch <= 0x07FF) { // non-ASCII <= 0x7FF
				sbuf.append(toHex(0xc0 | (ch >> 6)));
				sbuf.append(toHex(0x80 | (ch & 0x3F)));
			} else { // 0x7FF < ch <= 0xFFFF
				sbuf.append(toHex(0xe0 | (ch >> 12)));
				sbuf.append(toHex(0x80 | ((ch >> 6) & 0x3F)));
				sbuf.append(toHex(0x80 | (ch & 0x3F)));
			}
		}
		return sbuf.toString();
	}


	public String URLdecode(String s) {
		StringBuffer sbuf = new StringBuffer();
		int l = s.length();
		int ch = -1;
		int b, sumb = 0;
		for (int i = 0, more = -1; i < l; i++) {
			/* Get next byte b from URL segment s */
			switch (ch = s.charAt(i)) {
			case '%':
				ch = s.charAt(++i);

				int hb = (('0' <= ch && ch <= '9')) ? ch - '0' : (10 + Convert
						.toLowerCase((char) ch) - 'a') & 0xF;
				ch = s.charAt(++i);

				int lb = (('0' <= ch && ch <= '9')) ? ch - '0' : (10 + Convert
						.toLowerCase((char) ch) - 'a') & 0xF;
				b = (hb << 4) | lb;
				break;
			case '+':
				b = ' ';
				break;
			default:
				b = ch;
			}
			/* Decode byte b as UTF-8, sumb collects incomplete chars */
			if ((b & 0xc0) == 0x80) { // 10xxxxxx (continuation byte)
				sumb = (sumb << 6) | (b & 0x3f); // Add 6 bits to sumb
				if (--more == 0)
					sbuf.append((char) sumb); // Add char to sbuf
			} else if ((b & 0x80) == 0x00) { // 0xxxxxxx (yields 7 bits)
				sbuf.append((char) b); // Store in sbuf
			} else if ((b & 0xe0) == 0xc0) { // 110xxxxx (yields 5 bits)
				sumb = b & 0x1f;
				more = 1; // Expect 1 more byte
			} else if ((b & 0xf0) == 0xe0) { // 1110xxxx (yields 4 bits)
				sumb = b & 0x0f;
				more = 2; // Expect 2 more bytes
			} else if ((b & 0xf8) == 0xf0) { // 11110xxx (yields 3 bits)
				sumb = b & 0x07;
				more = 3; // Expect 3 more bytes
			} else if ((b & 0xfc) == 0xf8) { // 111110xx (yields 2 bits)
				sumb = b & 0x03;
				more = 4; // Expect 4 more bytes
			} else /* if ((b & 0xfe) == 0xfc) */{ // 1111110x (yields 1 bit)
				sumb = b & 0x01;
				more = 5; // Expect 5 more bytes
			}
			/* We don't test if the UTF-8 encoding is well-formed */
		}
		return sbuf.toString();
	}

	public boolean openConnection()	{

		sock = new Socket(host, xport);
		if (sock.isOpen()) {
			ds = new DataStream(sock);
			return true;
		}

		return false;
	}


	public boolean Disconnect()	{

		ds.close();
		sock = null;
		ds = null;
		buffer = null;
		return true;

	}

	public boolean isOpen(){
		return (sock != null);
	}


	public byte[] getContentByByte()	{
		String con = getContent();
		return con.getBytes();
	}


	public String getContent()	{

		ds.readBytes(buffer);
		String content = new String(buffer);
		int xpos = content.indexOf("\r\n\r\n");
		int pos;
		String xhead = content.substring(0, xpos);
		pos = xhead.indexOf("\r\n");
		content_headers.put("Status", xhead.substring(0, pos));
		xhead = xhead.substring(pos + 2);
		xhead = xhead + "\r\n";
		while (true) {
			String line;
			String key;
			String value;
			pos = xhead.indexOf("\r\n");
			if (pos == -1) {
				break;
			}
			line = xhead.substring(0, pos);

			xhead = xhead.substring(pos + 2);

			pos = line.indexOf(":");
			key = line.substring(0, pos);
			value = line.substring(pos + 2);

			content_headers.put(key, value);

		}
		read_headers = true;

		return content.substring(xpos + 4);

	}
	public int readContentChar(){
		byte[] buf = new byte[1];
		int n;

		n = ds.readBytes(buf);
		if (n == -1) {
			return -1;
		}

		return buf[0];
	}

	public String readhttpLine()	{
		StringBuffer buf = new StringBuffer();
		int ch;

		while (true) {
			ch = readContentChar();
			if (ch == -1) {
				if (buf.length() == 0) {
					return null;
				}
				return buf.toString();
			}
			if (ch == '\n') {
				buf.append((char) ch);
				return buf.toString();
			}
			buf.append((char) ch);

		}

	}

	public String readContentLine(){

		if (!read_headers) {
			String xhead = "";
			String xline;

			read_headers = true;
			while (true) {
				xline = readhttpLine();
				if (xline == null) {
					return null;
				}
				if (xline.equals("\r\n")) {
					int pos;
					pos = xhead.indexOf("\r\n");
					content_headers.put("Status", xhead.substring(0, pos));
					xhead = xhead.substring(pos + 2);
					while (true) {
						String line;
						String key;
						String value;
						pos = xhead.indexOf("\r\n");
						if (pos == -1) {
							break;
						}
						line = xhead.substring(0, pos);
						xhead = xhead.substring(pos + 2);

						pos = line.indexOf(":");
						key = line.substring(0, pos);
						value = line.substring(pos + 2);

						content_headers.put(key, value);

					}

					xhead = null;
					break;
				}
				xhead = xhead + xline;
			}

		}

		return readhttpLine();

	}

	public String getHost()	{
		return host;
	}

	public String getProtocol()	{
		return protocol;
	}

	public String getUri()	{
		return uri;
	}

	public String getContentHeaders(String name)	{
		return (String) content_headers.get(name);
	}


	public void add_header(String name, String value)	{
		headers.put(name, value);
	}

	public void remove_header(String name)	{
		headers.remove(name);
	}


	public String get_header(String name)	{
		return (String) headers.get(name);
	}


	public void add_parameter(String name, String value)	{
		parameters.put(name, value);
	}


	public void remove_parameter(String name)	{
		parameters.remove(name);
	}


	public String get_parameter(String name)	{
		return (String) parameters.get(name);
	}


	public void do_get()	{

		String postdata = "";

		Vector xkeys = parameters.getKeys();
		int sz = xkeys.size();

		if (sz > 0) {
			for (int x = 0; x < sz - 1; x++) {
				String key = (String) xkeys.get(x);
				postdata = postdata + key + "="
						+ URLencode((String) parameters.get(key)) + "&";
			}
			postdata = postdata + xkeys.get(sz - 1) + "="
					+ parameters.get(xkeys.get(sz - 1));

		}

		String header = "GET /" + uri + "?" + postdata + " HTTP/1.0\n";

		xkeys = headers.getKeys();
		sz = xkeys.size();

		if (sz > 0) {
			for (int x = 0; x < sz - 1; x++) {
				String key = (String) xkeys.get(x);
				header = header + key + ": " + headers.get(key) + "\n";
			}
			header = header + xkeys.get(sz - 1) + ": "
					+ headers.get(xkeys.get(sz - 1)) + "\n\n";

		} else {
			header = header + "\n";
		}

		ds.writeBytes(header.getBytes());

	}


	public void do_post()	{

		String postdata = "";

		Vector xkeys = parameters.getKeys();
		int sz = xkeys.size();

		if (sz > 0) {
			for (int x = 0; x < sz - 1; x++) {
				String key = (String) xkeys.get(x);
				postdata = postdata + key + "=" + parameters.get(key) + "&";
			}
			postdata = postdata + xkeys.get(sz - 1) + "="
					+ parameters.get(xkeys.get(sz - 1));

		}

		String header = "POST /" + uri + " HTTP/1.0\n"
				+ "Content-type: application/x-www-form-urlencoded\n"
				+ "Content-length: " + postdata.length() + "\n";

		xkeys = headers.getKeys();
		sz = xkeys.size();

		if (sz > 0) {
			for (int x = 0; x < sz - 1; x++) {
				String key = (String) xkeys.get(x);
				header = header + key + ": " + headers.get(key) + "\n";
			}
			header = header + xkeys.get(sz - 1) + ": "
					+ headers.get(xkeys.get(sz - 1)) + "\n\n";

		} else {
			header = header + "\n";
		}

		ds.writeBytes(header.getBytes());
		ds.writeBytes(postdata.getBytes());

	}

}

Bom pelo que vi no eclipse ele da erro nas linhas que contem o seguinte

xkeys.get(x);

Mas será que eu to fazendo algo errado ou tenho que mudar algo mesmo…

Tava olhando na API do Superwaba e a classe vector não contem o método get e nem um outro método que eu possa utilizar para pegar um objeto

2 Respostas

fiaux

Você preparou o ambiente para desenvolver em SuperWaba?

OBS: http://br.groups.yahoo.com/group/superwaba/

fabianofrizzo

Preparei o ambiente sim…

Mas tava olhando na Documentação e no fonte do SuperWaba e não encontrei o método get dentro da classe Vector…

Encontrei dentro da classe hasTable mas dai não funciona direito…

Obrigado pela sua atenção

Criado 25 de março de 2008
Ultima resposta 25 de mar. de 2008
Respostas 2
Participantes 2