2013-04-17 24 views
1

我有一个移动应用程序,用于搜索连接到Web服务的定义。目前的输出是以吐司的形式出现的,但我希望它出现在文本视图中,以使它看起来更好,更具可读性。如何从Web服务将文本输出转换为文本视图

public class Definition extends Activity { 

Button btnSend, btnSaveFave, btnReturn; 
EditText enterDefinition; 
//String wordRecieved; 


private InputStream OpenHttpConnection(String urlString) 
     throws IOException 
     { 
      InputStream in = null; 
      int response = -1; 

      URL url = new URL(urlString); 
      URLConnection conn = url.openConnection(); 

      if (!(conn instanceof HttpURLConnection))      
       throw new IOException("Not an HTTP connection");   
      try{ 
       HttpURLConnection httpConn = (HttpURLConnection) conn; 
       httpConn.setAllowUserInteraction(false); 
       httpConn.setInstanceFollowRedirects(true); 
       httpConn.setRequestMethod("GET"); 
       httpConn.connect(); 
       response = httpConn.getResponseCode();     
       if (response == HttpURLConnection.HTTP_OK) { 
        in = httpConn.getInputStream();         
       }      
      } 
      catch (Exception ex) 
      { 
       Log.d("Networking", ex.getLocalizedMessage()); 
       throw new IOException("Error connecting"); 
      } 
      return in;  
     } 

private String WordDefinition(String word) { 
InputStream in = null; 
String strDefinition = ""; 
//wordRecieved = word; 
try { 
    in = OpenHttpConnection(
    "http://services.aonaware.com/DictService/DictService.asmx/Define?word=" + word); 
    Document doc = null; 
    DocumentBuilderFactory dbf = 
     DocumentBuilderFactory.newInstance(); 
    DocumentBuilder db;    
    try { 
     db = dbf.newDocumentBuilder(); 
     doc = db.parse(in); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }    
    doc.getDocumentElement().normalize(); 

    //---retrieve all the <Definition> elements--- 
    NodeList definitionElements = 
     doc.getElementsByTagName("Definition"); 

    //---iterate through each <Definition> elements--- 
    for (int i = 0; i < definitionElements.getLength(); i++) { 
     Node itemNode = definitionElements.item(i); 
     if (itemNode.getNodeType() == Node.ELEMENT_NODE) 
     {    
      //---convert the Definition node into an Element--- 
      Element definitionElement = (Element) itemNode; 

      //---get all the <WordDefinition> elements under 
      // the <Definition> element--- 
      NodeList wordDefinitionElements = 
       (definitionElement).getElementsByTagName(
       "WordDefinition"); 

      strDefinition = ""; 
      //---iterate through each <WordDefinition> elements--- 
      for (int j = 0; j < wordDefinitionElements.getLength(); j++) {      
       //---convert a <WordDefinition> node into an Element--- 
       Element wordDefinitionElement = 
        (Element) wordDefinitionElements.item(j); 

       //---get all the child nodes under the 
       // <WordDefinition> element--- 
       NodeList textNodes = 
        ((Node) wordDefinitionElement).getChildNodes(); 

       strDefinition += 
        ((Node) textNodes.item(0)).getNodeValue() + ". \n";  
      } 

     } 
    } 
} catch (IOException e1) { 
    Log.d("NetworkingActivity", e1.getLocalizedMessage()); 
} 
//---return the definitions of the word--- 
return strDefinition; 
} 

private class AccessWebServiceTask extends AsyncTask<String, Void, String> { 
protected String doInBackground(String... urls) { 
    return WordDefinition(urls[0]); 
} 

protected void onPostExecute(String result) 
{ 
      //I think the this is where the output problem is however im unsure 
    //enterDefinition.setText(result); 
    Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); 
} 
} 
+0

在你的xml布局文件中放置一个文本视图,并在你的活动中处理它,并使用'textview.setText(result);设置结果'' – kamil

+0

事情是我不能访问方法之外的'result' 'onPostExecute' –

+0

我有我的回答,请检查 – kamil

回答

0

希望它会帮助你..

  public class Definition extends Activity { 
      TextView textView; 

      @Overide 
      public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.xyz); 
      textView = (TextView) findViewById(R.id.abc); 
      } 

现在只需拨打textView.setText(result)上后执行它的工作。