2012-07-25 36 views
0

即时通讯新手在Android应用程序开发中,我需要一些帮助。 我创建这个简单的词典应用程序,提示用户输入一个单词,并在按下按钮后将它接入互联网,可能wikipedia.org并将该信息返回给用户。 我使用XML来开发应用程序文本框和按钮。并创建了一段文本(ansa),将设置为使用OnClickListener的任何答案,我不想设置webview 我只想将文本设置为字典答案。 这是我迄今为止所能做到的。有这个类来从谷歌获取数据。如何从互联网获取具体信息Android应用程序

public class GetMethod { 

public String getInternetData() throws Exception{ 
    BufferedReader in = null; 
    String data = null; 
    try{ 
     HttpClient client = new DefaultHttpClient(); 
     URI website = new URI("http://www.google.com"); 
     HttpGet request = new HttpGet(); 
     request.setURI(website); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String l = ""; 
     String nl = System.getProperty("line.separator"); 
     while((l = in.readLine()) !=null){ 
      sb.append(l + nl); 
     } 
     in.close(); 
     data = sb.toString(); 
     return data; 
    }finally{ 
     if (in !=null){ 
      try{ 
       in.close(); 
       return data; 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

而在XML中实现

public class Dictionary extends Activity { 

TextView ansa; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dictionary); 

    Button Search = (Button) findViewById(R.id.search); 

    ansa = (TextView) findViewById(R.id.ansa); 

    final GetMethod test = new GetMethod(); 





    Search.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String returned; 
       try { 
        returned = test.getInternetData(); 
        ansa.setText(returned); 

       } catch (Exception e) { 
        ansa.setText("Failed"); 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 



     } 
    }); 






} 

} 当执行我得到整个网站的HTML

所以我需要如何把用户的话,以帮助另一类维基网站,只获取ansa的文本,可能解析并将其存储到某个字符串中。 非常感谢。

回答

2

您可以使用像Google Dictionarydictionary.com这样的API。

但是你将不得不实现HTTP客户端并解析响应。然后显示所需的数据。

+0

我将如何能够上传用户的单词?并将其解析为一个请求。 Thanx bro – 2012-07-25 21:00:38

+0

ansa.getText()。toString将返回以字符串形式输入的文本。您可以在getInternetData()方法中将该字符串追加到API调用中。那么它取决于什么样的数据格式API返回,如何解析它。 – Sayyam 2012-07-25 22:48:36

+0

让我知道你决定使用哪个API,然后我可以告诉你关于解析器。 – Sayyam 2012-07-25 22:59:56

相关问题