import java.net cannot be resolved error

1,348 views
Skip to first unread message

satish

unread,
Apr 23, 2009, 10:41:58 PM4/23/09
to Google Web Toolkit
I am using GWT plugin for eclipse and trying to build an test
application to be deployed on app engine. I am relatively new to Java
and App Engine. I have import java.net.* statement declared but when I
run the program in hosted browser mode, I get the error - Line 5: The
import java.net cannot be resolved. I don't get any error for java.io
or java.util.ArrayList imports. What am i doing wrong? Thank you.

Here is the program. getJoke() method needs classes from java.net.


package com.google.gwt.sample.stockwatcher.client;


import java.io.*;
import java.net.*;
import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;



public class StockWatcher implements EntryPoint {

private VerticalPanel mainPanel = new VerticalPanel();
private FlexTable stocksFlexTable = new FlexTable();
private HorizontalPanel addPanel = new HorizontalPanel();
private TextBox newSymbolTextBox = new TextBox();
private Button addStockButton = new Button("Add");
private Label lastUpdatedLabel = new Label();
private ArrayList<String> stocks = new ArrayList<String>();
private TextBox jokeTextBox = new TextBox();
private Button getJokeButton = new Button("Get Joke");
private HorizontalPanel jokePanel = new HorizontalPanel();

/**
* Entry point method.
*/
public void onModuleLoad() {
// Create table for stock data.
stocksFlexTable.setText(0, 0, "Symbol");
stocksFlexTable.setText(0, 1, "Price");
stocksFlexTable.setText(0, 2, "Change");
stocksFlexTable.setText(0, 3, "Remove");

// Assemble Add Stock panel.
addPanel.add(newSymbolTextBox);
addPanel.add(addStockButton);

//Assmeble Joke Panel
addPanel.add(getJokeButton);
addPanel.add(jokeTextBox);

// Assemble Main panel.
mainPanel.add(stocksFlexTable);
mainPanel.add(addPanel);
mainPanel.add(jokePanel);
mainPanel.add(lastUpdatedLabel);


// Associate the Main panel with the HTML host page.
RootPanel.get("stockList").add(mainPanel);

// Move cursor focus to the input box.
newSymbolTextBox.setFocus(true);
// Listen for mouse events on the Add button.
addStockButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
});

// Listen for keyboard events in the input box.
newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER) {
addStock();
}
}
});

// Listen for mouse events on the getJoke button.
getJokeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
getJoke();
}
});


}

/**
* Add stock to FlexTable. Executed when the user clicks the
addStockButton or
* presses enter in the newSymbolTextBox.
*/
private void addStock() {
final String symbol = newSymbolTextBox.getText().toUpperCase
().trim();
newSymbolTextBox.setFocus(true);

// Stock code must be between 1 and 10 chars that are numbers,
letters, or dots.
if (!symbol.matches("^[0-9a-zA-Z\\.]{1,10}$")) {
Window.alert("'" + symbol + "' is not a valid symbol.");
newSymbolTextBox.selectAll();
return;
}

newSymbolTextBox.setText("");

// Don't add the stock if it's already in the table.
if (stocks.contains(symbol))
return;

// Add the stock to the table.
int row = stocksFlexTable.getRowCount();
stocks.add(symbol);
stocksFlexTable.setText(row, 0, symbol);

// Add a button to remove this stock from the table.
Button removeStockButton = new Button("x");
removeStockButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int removedIndex = stocks.indexOf(symbol);
stocks.remove(removedIndex);
stocksFlexTable.removeRow(removedIndex + 1);
}
});
stocksFlexTable.setWidget(row, 3, removeStockButton);

// TODO Get the stock price.




}

private void getJoke() {
//jokeTextBox.setText("Joke Button Clicked");



try {
String message = URLEncoder.encode("joke", "UTF-8");
URL url = new URL("http://hellworld.appspot.com");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

OutputStreamWriter writer = new OutputStreamWriter
(connection.getOutputStream());
writer.write("request=" + message);
writer.close();

if (connection.getResponseCode() ==
HttpURLConnection.HTTP_OK) {
// OK
jokeTextBox.setText(connection.getResponseMessage());
} else {
// Server returned HTTP error code.
jokeTextBox.setText("Error");
}
} catch (MalformedURLException e) {
// ...
jokeTextBox.setText("Error");
} catch (IOException e) {
// ...
jokeTextBox.setText("Error");
}

}

}

Adam T

unread,
Apr 24, 2009, 12:56:17 AM4/24/09
to Google Web Toolkit
If this is client side code, you can't use java.net as the code gets
compiled to JavaScript - I'd suggest you read the documentation for
GWT to get a feel of what you can and can't do, in particular what is
included in the JRE Emulation for GWT:
http://code.google.com/intl/sv-SE/webtoolkit/doc/1.6/RefJreEmulation.html

You should also check you the RequestBuilder class which allows you to
make calls to a server from your code, BUT, as this will be from a
browser you will need to be aware of the Single Origin Policy - i.e.
your code can only make a call back to the server from which it came.
So in your case, if your GWT application is served from
hellworld.appspot.com your would be ok, if not, then you can't.

//Adam

satish

unread,
Apr 24, 2009, 9:56:56 AM4/24/09
to Google Web Toolkit
Thank you. I get it now.

On Apr 23, 9:56 pm, Adam T <adam.t...@gmail.com> wrote:
> If this is client side code, you can't use java.net as the code gets
> compiled to JavaScript - I'd suggest you read the documentation for
> GWT to get a feel of what you can and can't do, in particular what is
> included in the JRE Emulation for GWT:http://code.google.com/intl/sv-SE/webtoolkit/doc/1.6/RefJreEmulation....
Reply all
Reply to author
Forward
0 new messages