I'm trying to implement java URL into my gwt project. This is what I've done so far in terms of experimenting, trial-error, etc: HTML:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="Test_project.css">
<title>John Mathew's Google AJAX Search API Sample</title>
<script src="http://www.google.com/jsapi?key=*insert google api key*" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1');
google.setOnLoadCallback(function(){
var searchControl = new google.search.SearchControl();
searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
searchControl.addSearcher(new google.search.WebSearch());
var drawOptions = new google.search.DrawOptions();
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
searchControl.draw(document.getElementById("content"), drawOptions);
}, true);
</script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'en', style : google.loader.themes.SHINY});
google.setOnLoadCallback(function() {
var customSearchControl = new google.search.CustomSearchControl('*insert google cse id*');
CustomSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
}, true);
</script>
<script type="text/javascript" language="javascript" src="test_project/test_project.nocache.js"></script>
Loading... document.writeln('search engine 2');
Loading... document.writeln('search engine 3');<div id="customSearch"></div>
Now both parts of the javascript generates a google textbox and button. Upon entering information, it will return results. This isn't what I want. I would like to obtain the results for me to manipulate and play around with. So I did a bit of research and discovered PHP get_file_contents() and JAVA URL(). I've never had PHP experience so I went with Java (there is JQuery get() but I am confused on how to implement and couldn't find examples).
To understand how Java URL() works, I created a Java project and learned of this code:
URL urlsearch = new URL("*insert website*");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(urlsearch.openStream()));
String HTMLdisplay;
while ((HTMLdisplay = buffreader.readLine()) != null) {
System.out.println(HTMLdisplay);
}
buffreader.close();
It works exactly how I wanted as it retrieves the html page source of a page (example: http://www.bing.com/search?q=gygax). To implement this, I created a project based on how http://code.google.com/webtoolkit/doc/latest/tutorial/create.html instructs thus in the HTML code above there is the line . The command that links the two is in the class onModuleLoad():
// Associate the Main panel with the HTML host page.
RootPanel.get("customSearch").add(mainPanel);
This sounds a bit confusing so I'll summarize a bit: using Google Widgets on my .java I created a textbox and button when I run it as a web application. When you click the button, it is suppose execute the java url code and retrieve the pagesource on what it queries. Instead I get these errors:
[DEBUG] [test_project] - Validating newly compiled units
[ERROR] [test_project] - Errors in 'file:/C:/*file path*/Test_project.java'
[ERROR] [test_project] - Line 92: No source code is available for type java.net.URL; did you forget to inherit a required module?
[ERROR] [test_project] - Line 93: No source code is available for type java.io.BufferedReader; did you forget to inherit a required module?
[ERROR] [test_project] - Line 93: No source code is available for type java.io.InputStreamReader; did you forget to inherit a required module?
[ERROR] [test_project] - Line 99: No source code is available for type java.net.MalformedURLException; did you forget to inherit a required module?
[ERROR] [test_project] - Line 102: The method exit(int) is unde开发者_Go百科fined for the type System
[ERROR] [test_project] - Line 107: The method exit(int) is undefined for the type System
[TRACE] [test_project] - Finding entry point classes
[ERROR] [test_project] - Unable to find type 'com.example.test_project.client.Test_project'
[ERROR] [test_project] - Hint: Previous compiler errors may have made this type unavailable
[ERROR] [test_project] - Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[ERROR] [test_project] - Failed to load module 'test_project' from user agent 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729; .NET4.0E)' at localhost:3410
I have import java.io.* and import java.net.*
(have tried specific packages too) along with the many com.google.gwt
packages.
MY QUESTION: How do I overcome these errors that prevent me from using JAVA URL in my GWT project?
you cannot use java.io.*
or java.net
in GWT Project client side. As GWT does not provide equivalents to the above packages (except Serializable
).
I think Test_project.java
is in client side only, so that you are getting this error.
In the client side you can use only lang,util packages.
Google Web Toolkit doesn't support the entire JRE standard library; it emulates only certain classes and unfortunately for you, it doesn't have java.net.*
. It does however, have com.google.gwt.http.client. You should be using that.
You can make this work by moving the non-translatable code to a different location in your Java project.
Remember, GWT translates Java to javascript and runs in the browser. The GWT compiler targets anything beneath the module's client/ directory, that is. As no.good.at.coding mentioned, javascript lacks the java.io* and java.net* packages, so GWT can't make that part of your code into js.
However, you can use any library you want if you move the code into server-side .java files under server/rpc/ and expose it as a service. To wire up server-side rpc services with GWT, you will need to create a synchronous and asynchronous interface for your service, plus the implementation, e.g. (not checked for typos):
// rpc service interface, UrlGetServiceRpc.java:
@RemoteServiceRelativePath("urlget.rpc")
public interface UrlGetServiceRpc extends RemoteService {
String getPage(String url);
}
// async version of the interface, UrlGetServiceRpcAsync.java:
public interface UrlGetServiceRpcAsync {
void getPage(string url, AsyncCallback<String> async);
}
// implementation of the service, UrlGetServiceRpcImpl.java:
@Override
public string getPage(string url) {
// return page fetched using your code above.
}
// now in your page's module file... class level field:
private UrlGetServiceRpc urlGetSvc;
// in your page's constructor:
this.urlGetSvc = GWT.create(UrlGetServiceRpc.class);
// and when you want to display the html:
this.someWidget.setHtml(urlGetAsync.getPage("foo.com/goodreading.html"));
There are probably errors in the above, but it should get the general idea across. I also omit the import statements for incompatible packages from my GWT modules (client/* pages), and instead I use fully-qualified references in my methods (java.net.URL instead of just URL). This confines the scope of the non-GWTable package to the method level, and helps make sure the GWT compiler won't see the imports and whine.
Now you can also do get the html and display it asynchronously, by declaring the service as private UrlGetServiceRpcAsync urlSvc;
in your page, then calling the Async overload of the method. That's a bit beyond the scope of this question though; Google's "Communicating with a Server" article is a great place to start with understanding what runs where in a GWT app, and how to isolate things into the right layer.
精彩评论