我在网上找到了这个示例代码。它似乎可以做我想做的事,向 API 发出请求,我只需要对其进行一点定制。
但是,当我尝试编译它时,它给了我三行相同的错误
Syntax error on token(s), misplaced
construct(s)
- Syntax error on token "setEntity", =
expected after this token
也许有人能看到我看不到的东西?
这是代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class http {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
nameValuePairs.add 行和 httppost.setEntity 行引发错误