postData 无法解析为类型
我是 Android 编程新手。我正在尝试使用 post 将一些数据发布到服务器。我用谷歌搜索并想出了这个:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
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);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
我的问题是我在这段代码的第一行遇到错误:
- postData无法解析为
- 令牌“{”上的类型语法错误,删除
- 令牌“void”上的此令牌语法错误",@预计
我正在使用 Eclipse,并使用 Shift+Ctrl+o 来获取所有导入。
I'm new to Android programing. I'm trying to post some data to a server using post. I googled it up and came up with this:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
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);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
My problem is I'm getting errors on the first line of this code:
- postData cannot be resolved to a type
- syntax error on token "{", delete this token
- syntax error on token "void", @ expected
I'm using Eclipse and I used Shift+Ctrl+o to get all the imports.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题(根据您迄今为止提供的信息)是您在类之外声明函数
postData
。Java中的函数需要在类中声明。要么,您因使用过多的
}
而意外关闭了前一类(在这种情况下,您应该在额外的}
处出现错误),或者您还没有t 声明了一个类。该类可能看起来像这样:
You problem (based on the information you've given so far) is that you're declaring the function
postData
outside of a class.Functions in Java need to be declared in a class. Either, you've accidently close off the previous class by having one too many
}
(in which case you should have an error at the extra}
), or you haven't declared a class.The class could look something like this: