YD8877
YD8877

Reputation: 10800

Executing HTTP request inside a thread - Error

Following is the code of my activity I am using for sending an HTTP request to the server in a new thread. However, there is a syntactical error that I'm not able to figure out.

Following is the error I am getting

 Multiple markers at this line
    - Syntax error, insert ")" to complete       ClassInstanceCreationExpression
    - Syntax error, insert ";" to complete BlockStatements

I'm new to Java. Please help.

public class RegisterActivity extends Activity {

    private static final String TAG = "RegisterActivity";

    /*
     * Event listener for registration button. This will load the loading view and fire up the HTTP request to the server
     */
    public OnClickListener registrationBtnListener = new OnClickListener() {
        public void onClick(View v) {

            Thread trd = new Thread(new Runnable(){
                @Override
                public void run(){

                    String urlParameters = "";

                    try {
                        urlParameters = "fName=" + URLEncoder.encode("John", "UTF-8") +
                        "&lName=" + URLEncoder.encode("Smith", "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Http.post("http://192.168.1.101/project/test.php", urlParameters);
                }
            }
            trd.run();
        }
    };

EDIT :

Fixed by adding a ); before trd.run(). But now, I get a different error :

Multiple markers at this line
    - The method run() of type new Runnable(){} must override a superclass 
     method
    - implements java.lang.Runnable.run

Following is the code :

Thread trd = new Thread(new Runnable(){
                @Override
                public void run(){

                    String urlParameters = "";

                    try {
                        urlParameters = "fName=" + URLEncoder.encode("yash", "UTF-8") +
                        "&lName=" + URLEncoder.encode("desai", "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Http.post("http://192.168.1.101/bworld/test.php", urlParameters);
                }
            });

The error is on this line :

public void run(){

Upvotes: 0

Views: 609

Answers (2)

Stephen C
Stephen C

Reputation: 719661

Re second error. I think the problem is the @Override annotation. If you are compiling with Java 1.5 as source level, that annotation means that you need to override a method in a superclass. But you are (just) implementing a method in an interface.

Either remove the annotation, or change the source level to Java 1.6 or later.

Upvotes: 2

QuantumMechanic
QuantumMechanic

Reputation: 13946

You're missing the closing ) in new Thread(....

Upvotes: 2

Related Questions