在线程内执行HTTP请求 - 错误

时间:2012-04-29 01:40:16

标签: java android

以下是我用于在新线程中向服务器发送HTTP请求的活动代码。但是,有一个语法错误,我无法弄清楚。

以下是我得到的错误

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

我是Java的新手。请帮忙。

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

编辑:

通过添加a)修复;在trd.run()之前。但现在,我得到了一个不同的错误:

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

以下是代码:

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

错误在这一行:

public void run(){

2 个答案:

答案 0 :(得分:2)

您错过了)中的结束new Thread(...

答案 1 :(得分:2)

重新出错。我认为问题是@Override注释。如果使用Java 1.5作为源级别进行编译,则该批注意味着您需要覆盖超类中的方法。但是你(只是)在接口中实现一个方法。

删除注释,或将源级别更改为Java 1.6或更高版本。