2012-07-25 45 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

相關問題