关于java [英] about java

查看:78
本文介绍了关于java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连接到Wikipedia服务器以从应用程序中获取信息?

how to connect to Wikipedia server for information retrieval from an application?

推荐答案

您将必须使用java.net.URLjava.net.URLConnection类进行访问特定的URL,然后使用java.util.Scanner(带有java.io.InputStreamReader)读取该URL,以输出其HTML内容.这是一个可以为您完成此操作的简单方法.

You would have to use the java.net.URL and the java.net.URLConnection classes to access the particular URL and then read it using a java.util.Scanner (with java.io.InputStreamReader) to output its HTML content. Here is a simple method that can do that for you.

public String readWikiHTML(String topic, boolean raw) throws Exception
{
  String rawData = (raw) ? "?action=raw" : "";
  URLConnection connection = new URL(
    "http://en.wikipedia.org/wiki/" + 
    topic.replace(" ", "_") + 
    rawData).openConnection();
  String connectionType = connection.getContentType();
  int ndxCharset = connectionType.indexOf("charset=");
  Scanner scanner;
  if(ndxCharset > 0)
    scanner = new Scanner(
      new InputStreamReader(
        connection.getInputStream(), connectionType.substring(ndxCharset + 8)));
  else
      scanner = new Scanner(new InputStreamReader(connection.getInputStream()));
  scanner.useDelimiter("\\Z");
  String content = scanner.next();
  return content;
}



希望对您有所帮助!



I hope this helps!


[标准答案]
编写一些代码.

认真地说,您是否尝试过打开套接字并从URI中读取内容?去尝试一些事情,然后在卡住的时候问一个问题.
[Standard Answer]
Write some code.

Seriously, have you tried opening a socket and reading from the URI? Go and try something THEN ask a question when you''re stuck.


这篇关于关于java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆