我有以下导入
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.sshtools.j2ssh.net.HttpResponse;
在我的主要方法中,有以下代码
HttpClient client = new HttpClient();
HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("registrationid", "123456789"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
为了让 httpclient 导入来定位包,我必须从 java2s.com link here 下载 JAR 文件
但我仍然有多个错误
HttpPost cannot be resolved to a type
BasicNameValuePair cannot be resolved to a type
UrlEncodedFormEntity cannot be resolved to a type
它是一个 Ruby on Rails Web 服务,我需要能够:
创建在 WSDL 中定义的特定类型的对象。
调用服务,并将类型接收回我新定义的 对象。
不幸的是,我在 Java 开发方面经验不是很丰富。所以请记住这一点:)
非常感谢示例和指导。
谢谢
最佳答案
当您缺少 import 语句并且编译器无法在类路径上找到类型时,会出现错误 X 无法解析为类型
。这意味着您的类路径中缺少许多类文件。
类HttpPost
、BasicNameValuePair
、UrlEncodedFormEntity
都是Apache HttpComponents
的一部分。 .
下载二进制文件/源代码。在 zip 中,您将找到需要添加到类路径中的 jar。使用 Eclipse,您可以通过将 jar(作为外部 jar)添加到项目构建路径来完成此操作。
关于java - 想要使用Web服务但 "cannot be resolved to a type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018469/