4
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLEncoder;

import com.google.gson.Gson;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;

import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;   

public class SampleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageButton button = (ImageButton) findViewById(R.id.imageButton1);        
        button.setOnClickListener(new View.OnClickListener()   {             
           public void onClick(View v)  {               
            try {
                doSomething();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();                    
            }               
           }  
         });
       }
    public  void doSomething() throws Exception {
        EditText search2 = (EditText)findViewById(R.id.editText1);
        TextView urltext = (TextView)findViewById(R.id.textView1);
        String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
        String search = search2.toString() + "site:mysite.com";
        String charset = "UTF-8";

        URL url = new URL(google + URLEncoder.encode(search, charset));
        Reader reader = new InputStreamReader(url.openStream(), charset);
        GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
       String voidurlresult = results.getResponseData().getResults().get(0).getUrl().toString();
       urltext.setText(voidurlresult);           
    }
}

please take a look at the above code.....what's wrong with the code . When i click the button i get nothing. when i click the button i want to get the url of the first google result ...if someone can help me i will appreciate it

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
androiddevs78
  • 141
  • 2
  • 2
  • 9
  • 2
    try adding log messages or toast notification message to the `doSomething` method to ensure that the `doSomething` method is getting called. – slayton Oct 18 '11 at 21:08
  • what @slayton said would be a good test or just trying setting urltext.setText("Hey, my click worked!"); Likely your method to get data from the page isn't working as you intended – dymmeh Oct 18 '11 at 21:12
  • i change urltext.setText to something to test it but it does not appearing ... – androiddevs78 Oct 18 '11 at 22:20
  • i got the most code from http://stackoverflow.com/questions/3727662/how-can-you-search-google-programmatically-java-api/7811685#7811685 if this can help me somebody to help me! – androiddevs78 Oct 18 '11 at 23:06

5 Answers5

4
button.setOnClickListener(new View.OnClickListener()   {             
       public void onClick(View v)  {               
        try {
            SampleActivity.this.doSomething();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();                    
        }               
       }  
     });

Change the statement surrounded in the try statement to this. This will ensure that the right class is being referenced to call the method.

0

I guess I'm to late to the party, but it might help someone else.

You can't do a network operation on a main thread. Your code fails silently with NetworkOnMainThreadException raised by the system. If you run this code and check the LogCat output for your application you are going to notice the exception.

Solution is to do a network operation on a worker thread. You could use an AsyncTask in this use case.

Aleksandar Ilic
  • 1,521
  • 16
  • 19
0

Your code is correct, it just looks like there isn't any way for you to know the method is getting called. Consider adding some sort of notification even to your doSomething() method so you can know if the method gets called.

Do something like this:

public  void doSomething() throws Exception {

        // Show toast when method is called:
        Toast.makeText(this, "doSomething() is getting called", Toast.LENGTH_LONG).show();

        // the rest of doSomthing()


    }
slayton
  • 20,123
  • 10
  • 60
  • 89
0

While the code looks correct, the button click listener may not be getting set if an error is occurring before the set listener block. Wrap your code in an error trap to determine if an error is occurring. This is also good coding practice - especially when accessing external resources.

It may also be that the called method is erring and not completing the tasks. You should put a trap in that procedure as well.

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120
0

Sorry for my bad English.

Actually, you can get the current method's name with the StackTrace.

Here is a sample:

public class Test {

 public static void main(String args[]) {
    trace(Thread.currentThread().getStackTrace());
    new Test().doit();
    trace(Thread.currentThread().getStackTrace());
 }

 public void doit() {
    trace(Thread.currentThread().getStackTrace());
    doitagain();
 }

 public void doitagain() {
    trace(Thread.currentThread().getStackTrace());
 }

 public static void trace(StackTraceElement e[]) {
   boolean doNext = false;
   for (StackTraceElement s : e) {
       if (doNext) {
          System.out.println(s.getMethodName());
          return;
       }
       doNext = s.getMethodName().equals("getStackTrace");
   }
 }

}

I forgot to mention that this approach may not work in all circumstances.

AlenBer
  • 738
  • 2
  • 8
  • 25