以下是我用于在新线程中向服务器发送 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();
}
};
编辑 :
通过添加 ) 修复;在 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(){