0

im pretty new In Android App development, I need some help. Im creating this simple dictionary application that prompts the user to enter a word and after a button press it will take that word to the internet, probably wikipedia.org and return that information to the User. I used XML to develop the app textfield and button. And created a piece of text (ansa) which will be set to whatever the answer is using the OnClickListener, I do not want to set up a webview I just want the text to be set to the dictionary answer. Here's what i have been able to do so far. There is this class to get data from google.

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();
            }
        }
    }
}

And Another class where the XML is implemented

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();
                }



        }   
    });






}

} When it executes I get the whole website's HTML

So I need help on how to take the user's word to the wiki website, and get only the text of the ansa, probably parse and store it to some string. Thank You Alot.

Asiimwe
  • 2,199
  • 5
  • 24
  • 36

1 Answers1

2

You can use an API like Google Dictionary or dictionary.com.

But you will have to implement the HTTP client and parse the response. And then show the desired data .

Sayyam
  • 959
  • 10
  • 22
  • How would i be able to upload the user's word? and parse it into a request. Thanx bro – Asiimwe Jul 25 '12 at 21:00
  • ansa.getText().toString will return you the text entered by as string. You can append that string to the API call in your getInternetData() method. Then it depends on what data format API is returning that how you parse it. – Sayyam Jul 25 '12 at 22:48
  • Let me know which API you decided to use then I can tell you about the parser. – Sayyam Jul 25 '12 at 22:59
  • I thought so too.........Thank you. But what is this JSON? thingy? is there a list of JSON websites? – Asiimwe Jul 26 '12 at 08:46
  • JSON is data interchange format. It has key value pairs of data.You can use JSONObject class in android.[http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array] – Sayyam Jul 26 '12 at 13:24