开发者

Cannot getInputStream() in applet

Everyone,

I am trying to code an applet in Java which will access a video-game's Application Programming Interface (API), and 开发者_JS百科while I can successfully run the applet via the Eclipse IDE, it consistantly hangs up when run from the browser.

I've narrowed down where the bug must be by scattering debug messages around until I found the last line run.

I am attempting to parse the output from a parameter-filled URL. Figuring browsers must pass this information differently than an IDE, I've tried many different methods of doing this, including POSTing the parameters via http socket (although I am unfamiliar with this method and could easily have implemented it incorrectly). Below is my current version, irrelevent parts omitted (if you should deduce the bug might be in an omitted area, these are easily revealed):

...
URL apiCharList = null;
...
try {
    ...
    apiCharList = new URL("https:// ... ");
    URLConnection connection = apiCharList.openConnection();
    connection.connect();
    DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );
    BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
    String line = br.readLine();
    while( line != null ) {
        ...
        line = br.readLine();
    }
    ...
} catch (MalformedURLException e) {
    current.setText( "Malformed URL" );
} catch (IOException e) {
    current.setText( "InputStream fail." );
}

My debugging dropped me at:

connection.connect();

And without that line, at:

DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );

Any insight into this problem is most appreciated. Again, simply let me know if/which omitted areas may be necessary to see.

Respectfully, Inquiring

UPDATE

Thank you all for the replies. My BufferedReader now initializes as:

= new BufferedReader( new InputStreamReader( connection.getInputStream() ), 1 );

That part had been getting jumpbled up as a combination of all the various methods I had tried; thank you for ironing it out for me. From what I am seeing, it seems the issue is that my applet needs to be digitally signed in order to make the connections it requires when run via browser. I've been looking into that and have been having problems with keytool and jarsigner after downloading the latest JDK. I have only been at it for a short while, and have never had an applet digitally signed before, but at least I have a new avenue to pursue. If anyone caould provide a good (up to date) walkthrough on how to digitally sign an applet, that would be most appreciated. Various things I've read said it can cost me anywhere from 40USD to 20USD to nothing?

Anyways, thanks for the lift over this hurdle. Respectully, Inquiring


Browsers will not allow applets to make network connections anywhere but their own server of origin, by default. You would need to digitally sign the applet, then ask the user for permission to make the connection. An alternative is to run a simple proxy servlet on your web server which the applet can use to talk to the third-party server.

As an aside, why are you wrapping so many streams around eachother -- two of them buffered, which is a huge no-no -- just to read from the network? Those two lines could be replaced by

BufferedReader br = new BufferedReader( new InputStreamReader( connection.getInputStream() ), 1 );

Do note that ",1" I stuck in at the end, which turns off buffering for the BufferedReader. You do not want to read ahead on a network connection, or you're inviting your code to hang.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜